retagged by
10,813 views
18 votes
18 votes

What is printed by the following $\text{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;

}

$\text{ASCII}$ encoding for relevant characters is given below

$\begin {array}{|c|c|c|c|c|} \hline \text{A} & \text{B} & \text{C} & \dots & \text{Z} \\\hline 65 & 66 & 67 & \dots & 90 \\\hline  \end{array} \qquad \begin {array}{|c|c|c|c|c|} \hline \text{a} & \text{b} & \text{c} & \dots & \text{z} \\\hline 97 & 98 & 99 & \dots & 122 \\\hline  \end{array} \\ \qquad \qquad \qquad \quad \begin {array}{|c|c|c|} \hline  *  & + & –   \\\hline 42 & 43 & 45 \\\hline  \end{array} $

  1. $\text{z K S}$
  2. $122 \; 75 \; 83$
  3. $ * \; – \; + $
  4. $\text{P x +}$
retagged by

1 Answer

Best answer
19 votes
19 votes

Answer : Option A is the answer.


char a = ‘P’
char b = ‘x’

As per precedence of operators, () evaluated prior to +/-

Note that &,^ and | are bit wise operators and Addition/Subtraction of characters applied on their ASCII values.

a = ‘P’  ===> a = Ascii value of (‘P’) = 65+15 = 80 ==> a = (0101 0000)$_2$
b = ‘x’  ===> b = Ascii value of (‘x’) = 97+23 = 120 ==> b = (0111 1000)$_2$
 

a & b    = (0101 0000)$_2$  [ apply & logic on corresponding bits ] = (80)$_{10}$
‘*’ = 42 = (0010 1010)$_2$
a&b + ‘*’ = (0111 1010)$_2$ = 64+32+16+8+2 = 122    [ add corresponding bits ], [ simply 80+42 = 122 ]
print character ( 122 ) = small ‘z’

 

a | b    = (0111 1000)$_2$  [ apply | logic on corresponding bits ] = (120)$_{10}$
‘-’ = 45
a|b - ‘-’ = 120-45 = 75 = 65 + 10
print character ( 75 ) = Capital ‘K’

 

a ^ b    = (0010 1000)$_2$  [ apply ^ logic on corresponding bits ] = (40)$_{10}$
‘+’ = 43
a^b + ‘+’ = 40+43 = 83 = 65 + 18
print character ( 83 ) = Capital ‘S’

edited by
Answer:

Related questions

1 votes
1 votes
3 answers
3
admin asked Jan 5, 2019
373 views
What is the output of the following $\text{C}$ program?#include<stdio.h int main(void){ char s1[] = “Hello”; char s2[] = “World!”; s1 = s2; printf(“%s”,s1); }...