On Thu, 2010-12-02 at 16:48 -0800, Fred Cisin wrote:
#define SIX 1+5
#define NINE 8+1
printf("What you get when you multiply SIX by NINE: %d", (SIX * NINE) );
Which is why you should either bracket arithmetic operations in macros,
or declare them as 'const' variables:
const unsigned int SIX = 1+5;
#define NINE (8+1)
And your example gives the result 1 + (5*8) + 1 = (5*8)+2 = 42, because
of operator precedence rules (multiply first, then add). What you
probably wanted was 54, which is what you'd get if you fixed the
constants.
Whether this is a bug depends on what the code is meant to do. If it's
supposed to give you the answer for 1+(5*8)+1, then it's fine (though
I'd call it bad code because there's a significant potential for
confusion). If you wanted to know what 6*9 was, then you have a
problem...
"The mice will be furious."
Um, what?
--
Phil.
classiccmp at philpem.me.uk
http://www.philpem.me.uk/