Discussion
What is the output generated by the following code?
#define square(a) (a*a)
printf("%d", square(4+5));
Answer: C
Correct answer: 29
The macro has no parentheses round its parameter, so the argument is pasted in as raw text:
printf("%d", (4+5*4+5));
Multiplication binds tighter than addition, giving \(4 + 20 + 5 = 29\) rather than the intended \(9^2 = 81\). Parenthesising the parameter - #define square(a) ((a)*(a)) - is what fixes it.
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