Discussion
What will be the output of the program?
#include <stdio.h>
#define SWAP(a, b) int t; t=a, a=b, b=t;
int main()
{
int a=10, b=12;
SWAP(a, b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
Answer: B
Correct answer: a = 12, b = 10
The macro expands to int t; t=a, a=b, b=t; inside main, where a declaration followed by an expression statement is perfectly legal. The comma operator sequences the three assignments left to right, so the values are exchanged and the output is a = 12, b = 10. The macro would break if used twice in the same block, since t would be declared twice.
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