Advertisement

#define and vc++ is messin' with my head

Started by April 09, 2000 10:11 AM
8 comments, last by narlymon 24 years, 8 months ago
Okay, I''m screwing around with VC++ 6.0, trying to get a decent feel for it. This compiles fine: if(intZoomLevel == 1){ pCmdUI ->SetCheck(1); }else{ pCmdUI ->SetCheck(0); } However, if I do this: #define ZOOM_LOCAL 1; ... if(intZoomLevel == ZOOM_LOCAL){ pCmdUI ->SetCheck(1); }else{ pCmdUI ->SetCheck(0); } I get all these error messages: error C2143: syntax error : missing '')'' before '';'' error C2059: syntax error : '')'' warning C4390: '';'' : empty controlled statement found; is this the intent? error C2181: illegal else without matching if WTF?!?!? It doesn''t recognize the if anymore? Why not? Any and all help would be appreciated.
Don't end your define with a semicolon, you should type like this:
#define ZOOM_LOCAL    1 

instead of
#define ZOOM_LOCAL    1;




Edited by - DerekSaw on 4/9/00 10:20:03 AM
"after many years of singularity, i'm still searching on the event horizon"
Advertisement
Yep, the thing is that anything with a # in front of it is not actually C code -- it''s a preprocesor directive that tells the compiler to do certain things. So a ; is not neccasary since the preproccesor doesn''t use it.

--TheGoop
I should have seen that. . . .

Thanks
defines are always messy. this mistake happens to me all the time although i should know better
use constants instead

const int ciZoomLevel = 1;
Perhaps constants *are* better. Except for one thing...they take up space just like variables, whereas #defines just replace text before compiling.
Advertisement
merlin9x9, that actually isn't true in C++. Well, at least it shouldn't be true

Constants are 'filled in' at compile time (since they don't change), and therefore won't take up any resources.

Actually, using the (free) Borland C++ 5.5 compiler using a simple test program and examining the assembler source generated, it can be seen that the compiler uses the values of consts directly instead of allocating space in memory to hold the value.

Erik

Edited by - Erik Post on 4/9/00 3:31:07 PM
Actually, there isn''t any size difference. Either way the number has to be put in memory, so there isn''t any space saving.

--TheGoop
Oh, cool. I''d figured that was true but I''d read somewhere that they were implemented simply as variables except the compiler just made sure there weren''t any attempts to change it. Oh, well.
Well, it''s legal to take the address of a const variable. i.e.
const int two = 2;
const int * two_ref = &two
is valid code. In such cases the compiler is required to emit an entry in the data segment for the variable two. So consts variables will take up "additional" space in such an instance. Additional is in quotes, because, after all, you can''t do same thing with #define preprocessor directives.

This topic is closed to new replies.

Advertisement