Q31.Marks: +2.0UGC NET Paper 2: Computer Science 7th Dec 2023 Shift 2
What is the output of the following program?
#include <stdio.h >
# define SQR(x) (x*x)
int main()
{ int a, b = 3;
a=SQR(b+2);
printf("%d",a);
return 0;
}
1.25
2.11✓ Correct
3.Garbage value
4.24
Solution
The correct answer is 11
EXPLANATION:
The code defines a macro SQR(x) that squares its argument. The macro is defined as (x*x). When SQR(b+2) is evaluated, it becomes (b+2*b+2), not (b+2)*(b+2), because of how the preprocessor substitutes macro definitions.
#include <stdio.h >
# define SQR(x) (x*x)
int main()
{ int a, b = 3;
a=SQR(b+2);
printf("%d",a);
return 0;
}
So, given b=3, (b+2*b+2) will be equal to (3+2*3+2), which equals 11.