Home » C Programming » Bit Fields » Question
  1. What will be the output of the following C code?
    #include <stdio.h>
    union N
    {
    struct
    {
    unsigned char c : 2;
    unsigned int num : 2;
    }M;
    int c;
    };
    int main()
    {
    union N n.M = {2};
    printf("%d\n", n.M.c);
    }
    1. Garbage value
    2. Nothing
    3. Compilation Error
    4. 2
    5. None of these
Correct Option: C

Compilation Error

main.c: In function ‘main’:
main.c:13:18: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
union N n.M = {2};
^
main.c:13:18: error: expected expression before ‘.’ token
main.c:14:24: error: ‘n’ undeclared (first use in this function)
printf("%d\n", n.M.c);
^
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.