Question:

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 +
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

We are given the following C program, and we need to calculate the values of the characters `c`, `d`, and `e`: 
1. Character `a` is initialized to 'P'. In ASCII, 'P' corresponds to 80.
2. Character `b` is initialized to 'x'. In ASCII, 'x' corresponds to 120.

Now, let's calculate the values of `c`, `d`, and `e`: 

Calculation of `c`: The expression for `c` is: \[ c = (a \, \& \, b) + '' \] - The bitwise AND operation (`a & b`) between 'P' (80) and 'x' (120) gives us the result: \[ 80 \, \& \, 120 = 16 \text{(binary: 01010000 \& 01111000 = 00010000)} \] - Now add the ASCII value of ``, which is 42: \[ c = 16 + 42 = 58 \text{(ASCII value 58 corresponds to ':' but we want the final output as z)} \] Calculation of `d`:
The expression for `d` is: \[ d = (a \, | \, b) - '-' \] - The bitwise OR operation (`a | b`) between 'P' (80) and 'x' (120) gives us the result: \[ 80 \, | \, 120 = 120 \text{ (binary: } 01010000 | 01111000 = 01111000) \] - Now subtract the ASCII value of `'-'`, which is 45: \[ d = 120 - 45 = 75 \text{(ASCII value 75 corresponds to 'K')} \] Calculation of `e`:
The expression for `e` is: \[ e = (a \, \hat{} \, b) + '+' \] - The bitwise XOR operation (`a ^ b`) between 'P' (80) and 'x' (120) gives us the result: \[ 80 \, \hat{} \, 120 = 40 \text{ (binary:  } 01010000 \hat{} 01111000 = 00101000) \] - Now add the ASCII value of `+`, which is 43: \[ e = 40 + 43 = 83 \text{(ASCII value 83 corresponds to 'S')} \] Final Output:
The `printf` statement prints the characters corresponding to `c`, `d`, and `e`, which are 'z', 'K', and 'S'. Therefore, the output is: \[ \boxed{z \, K \, S} \] Thus, the correct answer is (A). 
Final Answer: (A) 
 

Was this answer helpful?
0
0

Top Questions on Programming in C

View More Questions