Discussion
What is the output of the following C program?
#include <stdio.h>
#define SQR(x) (x*x)
int main()
{
int a;
int b=4;
a=SQR(b+2);
printf("%d\n",a);
return 0;
}
Answer: A
Correct answer: 14
With b equal to 4, the unparenthesised macro expands to
a = b+2*b+2;
and multiplication binds tighter than addition: \(4 + 8 + 2 = 14\). The intended \(6^2 = 36\) requires the parameters to be parenthesised: #define SQR(x) ((x)*(x)).
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