Home » C Programming » Bit Fields » Question
  1. What will be the output of the following C code?
     #include <stdio.h>
    union Un
    {
    struct
    {
    unsigned char x : 2;
    unsigned int y : 2;
    }s;
    int x;
    };
    int main()
    {
    union Un u.s.x = 2;
    printf("%d\n", Un.s.x);
    }
    1. Compilation Error
    2. Garbage value
    3. 2
    4. Nothing
    5. Undefined behaviour
Correct Option: A

Compilation Error

main.c: In function ‘main’:
main.c:13:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
union Un u.s.x = 2;
^
main.c:13:19: error: expected expression before ‘.’ token
main.c:14:24: error: ‘Un’ undeclared (first use in this function)
printf("%d\n", Un.s.x);
^~
main.c:14:24: note: each undeclared identifier is reported only once for each function it appears in



Your comments will be displayed only after manual approval.