0

Explaination for #define function.

#define sqr(X) X*X int main() { printf("%d",sqr(3+1)); } Ans = 9 I don't know that how it is executing. please help me anyone here.

27th Oct 2018, 9:52 AM
Pavithran
Pavithran - avatar
3 Réponses
+ 1
You actually get 7. Explanation: 3+1 is actually pasted into X, since macros do work like text substitution. So it becomes: sqr(3+1)=3+1*3+1=7 Solution: wrap every argument of the macro with parentheses, like: #define sqr(X) (X)*(X) In that case it will be: (3+1)*(3+1)=4*4=16
27th Oct 2018, 10:14 AM
fra
fra - avatar
0
Sir thanks for replay. I tried this program in challenge.But I got that answer as "9". I just reported as "wrong answer".
27th Oct 2018, 10:49 AM
Pavithran
Pavithran - avatar
0
You're very welcome :)
27th Oct 2018, 10:57 AM
fra
fra - avatar