Forum
Wrong #defined calculated value - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16)
+---- Thread: Wrong #defined calculated value (/showthread.php?tid=264)



Wrong #defined calculated value - programandala.net - 06-21-2010

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

cls
print "Expected values in cramps:"
print
print "screenFirstCol(0)=";screenFirstCol
print "screenLastCol(31)=";screenLastCol
print "borderFirstCol(0)=";borderFirstCol
print "borderLastCol(31)=";borderLastCol
print "oWinFirstCol(1)=";oWinFirstCol
print "oWinLastCol(30)=";oWinLastCol
' Wrong result, 32 instead of 30:
print "oWinWidth(30)=";oWinWidth

pause 0
stop

What's the problem?

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?


Re: Wrong #defined calculated value - britlion - 06-22-2010

Not sure what's going on there. I get the same result.

It can't be because it's three things -

Code:
#define test 1+2+3
print test

comes back with 6, correctly.

The ic basically shows "print 32" as that last line that should be 30, so I can't easily drill down any further - the preprocessor is definitely making that set into a 32 though.


Re: Wrong #defined calculated value - boriel - 06-22-2010

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).


Re: Wrong #defined calculated value - programandala.net - 06-22-2010

boriel Wrote:The preprocessor does not calculate, just replace labels with code. With parenthesis:

I see. The label is not replaced by the calculation, but by its raw content. Thank you.