0
How the output is 10 but not 15?
#include <stdio.h> #define prod(i, j) i * j int main() { int x = 3, y = 4; printf("%d", prod(x + 2, y - 1)); return 0; }
8 Answers
+ 6
x+2*y-1=3+2*4-1=3+8-1=10
+ 5
NIKHIL JOSHI (ÂŽâȘÏâȘ`) Note: Pre-processor directives just replace the text. Do not compare it with functions.
+ 1
Martin Taylor what is the benefit of this method(replacing oprations by preprocessor) comparing to functions?
+ 1
Mehran I use macros when trying to make small functions, the job of whom can be done by simple text substitution, which is better in my opinion.
+ 1
If the preprocessor directive,
#define prod(i, j) i * j
were written the safe way, with parentheses, as,
#define prod(i, j) ((i)*(j))
the preprocessor would replace
printf("%d", prod(x + 2, y - 1));
with
printf("%d", ((x + 2) * (y - 1)));
and the result would be 15.
0
Bcuz of precedence of operator