How the output is 10 but not 15? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

16th Aug 2021, 7:55 PM
NIKHIL JOSHI
NIKHIL JOSHI - avatar
6 Answers
+ 6
x+2*y-1=3+2*4-1=3+8-1=10
16th Aug 2021, 8:00 PM
Abhay
Abhay - avatar
+ 5
NIKHIL JOSHI (´✪ω✪`) Note: Pre-processor directives just replace the text. Do not compare it with functions.
16th Aug 2021, 8:09 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Martin Taylor what is the benefit of this method(replacing oprations by preprocessor) comparing to functions?
17th Aug 2021, 12:14 AM
Mehran
Mehran - avatar
+ 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.
17th Aug 2021, 12:14 PM
Calvin Thomas
Calvin Thomas - avatar
+ 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.
16th Jun 2023, 11:36 PM
Rain
Rain - avatar
0
Bcuz of precedence of operator
18th Aug 2021, 9:54 AM
MD ZAHID
MD ZAHID - avatar