What is printed by the following ANSI C program?
#include\(\lt\)stdio.h\(\gt\)
int main(int argc, char *argv[]){
char a = 'P';
char b = 'x';
char c = (a & b) + '*';
char d = (a | b) - '-';
char e = (a ^ b) + '+';
printf("%c %c %c\n", c, d, e);
return 0;
}
ASCII encoding for relevant characters is given below
| A | B | C | ... | Z |
| 65 | 66 | 67 | ... | 90 |
| a | b | c | ... | z |
| 97 | 98 | 99 | ... | 122 |
| * | + | - |
| 42 | 43 | 45 |
GATE CS - 2022,
Answer: A
The program prints z K S.
'P' is 80 = 0101 0000 and 'x' is 120 = 0111 1000 in binary.
- a & b = 0101 0000 = 80; adding '*' (42) gives 122, which is 'z'.
- a | b = 0111 1000 = 120; subtracting '-' (45) gives 75, which is 'K'.
- a ^ b = 0010 1000 = 40; adding '+' (43) gives 83, which is 'S'.
The format %c prints characters, not numbers, so '122 75 83' is the right values in the wrong form. '* - +' and 'P x +' ignore the bitwise operations and simply echo the operand characters.
Please log in or register to report this question.