I am new to C language, and I meet the problem with the printf. Here is the code:
#include <stdio.h>
#define add1(x) ((x) = ((x)+1))
int main()
{
int x, y, temp;
x = 5;
printf("Add 1 to %d is equal to: %d", x, add1(x));
return 0;
}
After compiling and running this code, it shows the result:
Add 1 to 6 is equal to 6
What I expect is: "Add 1 to 5 is equal to 6"
That is, what I expect is that the behavior of it will show the format specifier by order: "Add 1 to x" (x = 5), and then "is equal to: add1(x)" (x = x+1 --> x=6) However, it seems that the compiler will compile add(x) simultaneously and directly make x = 6 while running to the line "printf("Add 1 to %d is equal to: %d", x, add1(x));"
Can someone help explain this problem with more detail? Thanks~