Assume that ints are 32-bit wide.
What happens if you try to compile and run this program?
#include <stdio.h>
typedef union {
int i;
int j;
int k;
} uni;
int main (int argc, char *argv[]) {
uni s;
s.i = 3;
s.j = 2;
s.k = 1;
printf("%d",s.k * (s.i – s.j));
return 0;
}
Choose the right answer:
A . The program outputs 9
B . The program outputs 0
C . Execution fails
D . Compilation fails
E . The program outputs 3
Answer: B
Explanation:
The program defines a union named uni with three members: i, j, and k. The members share the same memory location. The values are assigned to s.i, s.j, and s.k, but since they share the same memory, the value of s.i will overwrite the values of s.j and s.k.
So, s.i will be 3, and both s.j and s.k will be 3. Then, the expression s.k * (s.i – s.j) be-comes 3 * (3 – 3), which equals 0. The printf statement prints the result, and the pro-gram outputs 0.
The program is a valid C program that can be compiled and run without errors. The program defines a union type named uni that contains three int members: i, j, and k. Then it creates a variable of type uni named s and assigns values to its members. However, since a union can only hold one member value at a time, the last assignment (s.k = 1) overwrites the previous values of s.i and s.j. Therefore, all the members of s have the same value of 1. The program then prints the value of s.k * (s.i – s.j), which is 1 * (1 – 1) = 0. Therefore, the program outputs 0. References = C Unions – GeeksforGeeks, C Unions (With Examples) – Programiz, C – Unions – Online Tutorials Library
Latest CLA-11-03 Dumps Valid Version with 40 Q&As
Latest And Valid Q&A | Instant Download | Once Fail, Full Refund