[prog] C code
Kathryn Hogg
kjh at flyballdogs.com
Mon May 2 15:16:45 EST 2005
Noir said:
> Can anyone help me understand the following C code?
> Like, why the output is 4.
>
> #include <stdio.h>
> #define SIX 1 + 1
> #define NINE 2 + 1
> int main(void)
> {
> printf( "What you get if you multiply six by nine:
> %d\n",
> SIX * NINE
> );
> return 0;
> }
SIX * NINE is expanded by the preprocessor as
1 + 1 * 2 + 1
* has a higher precedence than + so that evaluates as
1 + (1 * 2) + 1
You could do your defines as
#define SIX (1+1)
#define NINE (2+1)
which would make your expression evaluate to (1+1) * (2+1) which is 6.
--
Kathryn
http://womensfooty.com
More information about the Programming
mailing list