Mail Archives: djgpp/2001/12/27/17:42:20
> #define BASEVAL1 233
> #define BASEVAL2 BASEVAL1 + 29
> #define BASEVAL3 233
> #define BASEVAL4 BASEVAL1 + 57
>
> printf("baseval4 - baseval2 [28] = %d\n\n", BASEVAL4 - BASEVAL2);
Use "gcc -E" and you'll see what's happening:
BASEVAL4 - BASEVAL2
BASEVAL1 + 57 - BASEVAL1 + 29
233 + 57 - 233 + 29
Yup, 57 + 29 is 86, not 28.
I suggest paranoid parens in your defines:
#define BASEVAL1 233
#define BASEVAL2 (BASEVAL1 + 29)
#define BASEVAL3 233
#define BASEVAL4 (BASEVAL1 + 57)
You should always use parens (1) around use of arguments in a #define
taking arguments, and (2) around the whole #define's value, when it's
an expression. Example:
#define TWICE(A) ((A)+2)
The inner parens are in case you do TWICE(y^4), the outer parens are
in case you do TWICE(j)*4
- Raw text -