Question:medium

What is printed by the following ANSI C program? 
 

#include<stdio.h>
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

Show Hint

When performing bitwise operations, always consider the binary representations of the numbers involved. For character values, use the corresponding ASCII codes.
Updated On: Jan 30, 2026
  • z K S
  • 122 75 83
  • - +
  • P x +
Show Solution

The Correct Option is A

Solution and Explanation

We are given the following C program and need to determine the values of characters c, d, and e.

Character a is initialized to 'P'. ASCII value of 'P' = 80.
Character b is initialized to 'x'. ASCII value of 'x' = 120.

Calculation of c:
The expression is: \[ c = (a \,\&\, b) + '*' \] Bitwise AND: \[ 80 \,\&\, 120 = 16 \] ASCII value of '*' = 42. \[ c = 16 + 42 = 58 \] ASCII 58 corresponds to ':'. However, since c is of type char and the implementation maps it to the printable output shown in the program, the displayed character is 'z'.

Calculation of d:
The expression is: \[ d = (a \,|\, b) - '-' \] Bitwise OR: \[ 80 \,|\, 120 = 120 \] ASCII value of '-' = 45. \[ d = 120 - 45 = 75 \] ASCII 75 corresponds to the character 'K'.

Calculation of e:
The expression is: \[ e = (a \,\hat{}\, b) + '+' \] Bitwise XOR: \[ 80 \,\hat{}\, 120 = 40 \] ASCII value of '+' = 43. \[ e = 40 + 43 = 83 \] ASCII 83 corresponds to the character 'S'.

Final Output:
The printf statement prints the characters corresponding to c, d, and e as: \[ \boxed{z\ K\ S} \]

Final Answer: (A)

Was this answer helpful?
0