Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

ABC...Z
656667...90
abc...z
979899...122
*+-
424345

  • A.z K S
  • B.122 75 83
  • C.* - +
  • D.P x +

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.