Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wrong #defined calculated value
#3
programandala.net Wrote:I think there's something I'm missing about the way #define values are calculated, look:
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
Let me check it:
  • borderFirstCol == 0
  • borderLasCol == 31
  • oWinFirstCol == 0 + 1 // Note: always expanded
  • oWinLastCol == 31 - 1
  • oWinWidth == 31 - 1 - 0 + 1 + 1 // This is the defined result
So oWinWidth == 32
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).
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)