in Programming retagged ago by
2,516 views
6 votes
6 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 +}$
in Programming retagged ago by
by
2.5k views

2 Comments

There’s an error in the question:

It should be char a = ‘P’ instead of char a = ‘p’
1
1
fixed 👍
0
0

1 Answer

7 votes
7 votes
Best answer

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 ( 75 ) = Capital ‘S’

edited by

4 Comments

what is the logic if the option b is correct? @Shaik Masthan sir

0
0

@Nisha Bharti It should’ve been:-

 

printf(“%d %d %d\n”, c, d, e); //printing integers rather than char

 

1
1

@Abhrajyoti00 yes thanku

1
1
Answer:

Related questions