-
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);
}
-
- Compilation Error
- Garbage value
- 2
- Nothing
- 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