Predict the output in case of union
union test{ int x; char arr[4]; int y;};int main(){ union test t; t.x = 0; t.arr[1] = 'G'; printf("%s", t.arr); return 0;}Answer:
Since x and arr[4] share the same memory, when we set x = 0, all characters of arr are set as 0. O is ASCII value of ''. When we do "t.arr[1] = 'G'", arr[] becomes "G". When we print a string using "%s", the printf function starts from the first character and keeps printing till it finds a . Since the first character itself is , nothing is printed.

Comments
Post a Comment