In C (not C++), what is the status of storing a value in one union member and then reading a different member of the same size?
A
Undefined behaviour in C as well, exactly as it is in C++, so the compiler may assume it never happens
B
A constraint violation, so a conforming compiler must reject the program instead of silently reinterpreting it
C
Permitted; the bytes are reinterpreted and the result is implementation-defined, which is called type punning
Correct answer
D
A conversion that applies the usual arithmetic conversions to the stored bytes before reading them back
Correct answer
C. Permitted; the bytes are reinterpreted and the result is implementation-defined, which is called type punning
16 views
⚔️
Challenge a friend on C Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
Explanation
Type punning through a union is allowed in C, where the result depends on the object representation and hence on endianness and padding, while C++ restricts it to the common initial sequence of standard-layout structs. A union holding a float and a uint32_t is thus a legal C reinterpretation but formally undefined in C++. memcpy is the portable alternative in both languages.
Reference
Asked in: NTS, PPSC IT, Campus Interviews
Doubts & discussionNot sure why the answer is C? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ What does the C standard guarantee about the return value of malloc(0)? Next MCQ › For char s[] = "hello"; what are the values of sizeof s and strlen(s)?