06-22-2010, 07:42 PM
programandala.net Wrote:I think there's something I'm missing about the way #define values are calculated, look:Let me check it:
Code:#define screenFirstCol 0
#define screenLastCol 31
#define borderFirstCol screenFirstCol
#define borderLastCol screenLastCol
#define oWinFirstCol borderFirstCol+1
#define oWinLastCol borderLastCol-1
' Wrong result, 32 instead of 30:
#define oWinWidth oWinLastCol-oWinFirstCol+1
- borderFirstCol == 0
- borderLasCol == 31
- oWinFirstCol == 0 + 1 // Note: always expanded
- oWinLastCol == 31 - 1
- oWinWidth == 31 - 1 - 0 + 1 + 1 // This is the defined result
Try using parenthesis in the #defines. The preprocessor does not calculate, just replace labels with code. With parenthesis:
Code:
#define screenFirstCol 0
#define screenLastCol 31
#define borderFirstCol screenFirstCol
#define borderLastCol screenLastCol
#define oWinFirstCol (borderFirstCol+1)
#define oWinLastCol (borderLastCol-1)
' Wrong result, 32 instead of 30:
#define oWinWidth (oWinLastCol-oWinFirstCol+1)
This gives oWinWidth == ((31 - 1) - (0 + 1) + 1) which effectively is reduced to 30.
This also happens in C.
Quote:I think #defined values are simple labels calculated by the preprocessor. Is it right? Is there any limit about the depth of the calculated values?No limit (only available memory).