Discussion
Predict the output of the program below. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
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: A
Correct answer: Nothing is printed
t.x = 0 writes zeros over all four bytes, so arr[0] becomes '\0'. Setting arr[1] to 'G' changes the second byte only, leaving the first still null. %s stops at the first null, so it prints nothing at all - the 'G' is there in memory but never reached.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development