Consider the following program #include <stdio.h> int main() { int x = 1;…
2018
Consider the following program
#include <stdio.h>
int main()
{
int x = 1;
printf("%d", *(char *)&x);
return 0;
}Assuming required header files are included and if the machine in which this program is executed is little-endian, then the output will be
- A.
0
- B.
99999999
- C.
1
- D.
none of these.
Attempted by 319 students.
Show answer & explanation
Correct answer: C
On a little-endian machine, the least significant byte of an integer is stored at the lowest memory address. The integer 1 (0x00000001) is stored such that the byte '01' is at the lowest address. Casting the address of x to a char pointer and dereferencing it retrieves this least significant byte, which is 1.