Please refer to the comments, Explain the third printf statement. Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <stdio.h>
#define x 12
#define y 7
#define z 5
#define w y+z
int main() {
printf("%d\n",x*x); // displays 144 //ok
printf("%d\n",w); // displays 12 // ok
printf("%d\n", w*w) ; // displays 47. Shouldn't this be 144?
return(0);
}
FWIW, this is one of the many, many reasons why you should avoid using #defines unless they are your only option.
This would be better:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <cstdio>
/*#define x 12 <- get rid of all this
#define y 7
#define z 5
#define w y+z
*/
constint x = 12; // <- replace with this
constint y = 7;
constint z = 5;
constint w = y+z;
int main() {
printf("%d\n",x*x); // displays 144 //ok
printf("%d\n",w); // displays 12 // ok
printf("%d\n", w*w) ; // displays 144 as you'd expect
return 0;
}