Constants in C Recently we had a debate in our office about defining numeric constants in C. One person thinks that it's better to define a numeric constant as a bitwise operation *if possible* like this: #define NUMMAX (1<<15) because it's easier to understand where it's derived from, *unless* it's a more complex number where the operation would include more than just a bit shift like this: #define NUMMAX ((1<<15) - 347) then, he would just use the number (as in #define NUMMAX 32421). Another person thinks it's fine to just use the base 10 number all the time like this: #define NUMMAX 32768 What do you think? Wayne Monday, August 11, 2003 Well that's easy.  Depends... if it is going to be used like a bitfield, then 1<<15 or 0x8000 instantly conveys that... if it's just a number that happens to be 32768 or 16, then use that. If you have a max of 16 users or something, don't define it as 1<<4 simply because you can -- that only confuses the reader.  But if's the 4th entry of a bitfield, then why not use 1<<4 to give them an extra hint. It's solely a matter of style... It doesn't really matter. Everyone should know what's going on in either case, but I would question why someone would not follow this obvious rule. Andy Monday, August 11, 2003 How about // // NUMMAX: 32421 == ((1<<15) - 347) // // 347 is the ____ constant, which means ____. // #define NUMMAX 32421 -- ee eclectic_echidna Monday, August 11, 2003 >>> because it's easier to understand where it's derived from <<< It should also make it easier to avoid mistakes in complex calculations. Did any of the advocates of versions 2 or 3 give any supporting rationale? Option 2 has some slight hazard that a rookie C programmer would enter it as #define NUMMAX    1<<15 - 347 Other than that, I don't see much reason to not write out the numeric calculation explicitly and let the compiler do the arithemetic, even for moderately complex calculations. mackinac Monday, August 11, 2003 It's been a very long while since I did any C but don't #define's get spirited away when using a debugger? Or have the newer ones figured it out yet? I remember stepping thru' code in some file and coming across a macro wouldn't tell me the resulting value - no address to go look up. I could be wrong - it has been a long time... macro Monday, August 11, 2003 I tend to distinguish number constants from bit-oriented constants mentally, and so I will define them differently.  Numbers get assigned a base10 integer value; bit-oriented constants get a base16 value.  It's easy (for me, anyway) to look at a hex value and see which bits are 1s and which are 0s.  -Thomas Thomas Monday, August 11, 2003 The debugger I work with, MULTI from Green Hills Software, will display the value of macros when debugging code, let you go the their definition, and in the beta version will evaluate macros with arguments. MULTI man Monday, August 11, 2003 I use the same conventation as Thomas. As for macros in debuggers, MSVC7 (VS.NET) will show tooltips of macros (and their definitions). runtime Monday, August 11, 2003 Thanks all.  We agree that it depends on what context the constant is originally defined for. Wayne Monday, August 11, 2003   Fog Creek Home