What is the output of the following ‘c’ code assuming it runs on a byte…
2020
What is the output of the following ‘c’ code assuming it runs on a byte addressed little endian machine?
C#include <stdio.h>
int main( )
{
int x; char *ptr;
x = 622,100,101;
printf("%d", (*(char *)&x) * (x % 3));
return 0;
}
- A.
622
- B.
311
- C.
22
- D.
110
Attempted by 205 students.
Show answer & explanation
Correct answer: D
In C, the assignment operator (=) has higher precedence than the comma operator (,). Therefore, the statement 'x = 622, 100, 101;' is evaluated as '(x = 622), 100, 101;'. The variable x is assigned the value 622. On a little-endian machine, the least significant byte of an integer is stored at the lowest memory address. The hexadecimal representation of 622 is 0x26E, so the least significant byte is 0x6E (110 in decimal). Thus, (*(char *)&x) equals 110. Additionally, 622 modulo 3 is 1 (since the sum of digits 6+2+2=10, and 10%3=1). The final output is calculated as 110 * 1, which equals 110.