What is the output of the following C program? #include <stdio.h> #define…
2024
What is the output of the following C program?
#include <stdio.h>
#define prod(a,b) a*b
int main()
{
int x=3,y=4;
printf("%d",prod(x+2,y-1));
return 0;
}Answer: A. 10 — ConceptA function-like C preprocessor macro performs textual substitution before compilation; it does not automatically add parentheses around its parameters…
- A.
10
- B.
20
- C.
15
- D.
0
Attempted by 242 students.
Show answer & explanation
Correct answer: A
Concept
A function-like C preprocessor macro performs textual substitution before compilation; it does not automatically add parentheses around its parameters or replacement body.
After expansion, the resulting C expression follows normal operator-precedence rules, where multiplication precedes addition and subtraction.
Application
Substitute the arguments into a*b: prod(x+2,y-1) expands textually to x+2*y-1.
Substitute x = 3 and y = 4 to obtain 3+2*4-1.
Apply multiplication first: 2*4 = 8.
Evaluate left to right at the addition/subtraction level: 3+8-1 = 10.
Cross-check
If the macro had been defined safely as ((a)*(b)), the call would produce (3+2)*(4-1)=15. That different result confirms why the missing parentheses change the output.
Result
The program prints 10.