Discussion
What will be the output of the program?
#include <stdio.h>
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
Answer: C
Correct answer: 27, 6
CUBE(b++) expands to (b++*b++*b++), which modifies b three times in one expression with no sequence point between - formally undefined behaviour. The listed answer, 27 and 6, is what the common compilers give: the three operands all read 3 before any increment takes effect, and b ends three higher. Nothing should rely on it; the real lesson is never to pass an argument with a side effect to a macro that uses it more than once.
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