Discussion
What will be the output of the following C code?
#include <stdio.h>
void main()
{
int x = 97;
int y = sizeof(x++);
printf("x is %d", x);
}
Answer: A
Correct answer: x is 97
sizeof does not evaluate its operand. The compiler needs only the type of the expression to determine its size, and that is known at compile time, so x++ is never executed.
Consequently y receives the size of an int and x remains 97. This is a genuine and easily missed rule: any side effect written inside sizeof - an increment, an assignment, even a function call - simply does not happen. The one exception is a variable-length array type, whose size must be computed at run time.
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