All mock tests are FREE till 31 December — register to save your progress

What is the output of the following C program? (Assume little-endian representation, with the least significant byte at the lowest address, and a two-byte short int.)

#include <stdio.h>
int main()
{
    union saving {
        short int one;
        char two[2];
    };
    union saving m;
    m.two[0] = 5;
    m.two[1] = 2;
    printf("%d, %d, %d\n", m.two[0], m.two[1], m.one);
}

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. 5, 2, 517

Explanation

two[0] is the low byte and two[1] the high byte of one, so one = 2 × 256 + 5 = 517.

More Programming Languages and Computer Graphics questions