What is printed by the following ANSI C program? #include<stdio.h> int…

2022

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

ABC...Z65666790

abc...z979899122

*+-424345

  1. A.

    z K S

  2. B.

    122 75 83

  3. C.

    * - +

  4. D.

    P x +

Attempted by 148 students.

Show answer & explanation

Correct answer: A

Answer: z K S

Key ASCII values used: 'P' = 80, 'x' = 120, '*' = 42, '-' = 45, '+' = 43.

  • For c = (a & b) + '*': a & b = 80 & 120 = 80. Then 80 + 42 = 122, which is 'z'.

  • For d = (a | b) - '-': a | b = 80 | 120 = 120. Then 120 - 45 = 75, which is 'K'.

  • For e = (a ^ b) + '+': a ^ b = 80 ^ 120 = 40. Then 40 + 43 = 83, which is 'S'.

printf uses %c, so it prints the characters separated by spaces: z K S

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir