#### Archived

This topic is now archived and is closed to further replies.

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

## Recommended Posts

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.

##### Share on other sites
Don't end your define with a semicolon, you should type like this:
#define ZOOM_LOCAL    1

#define ZOOM_LOCAL    1;

Edited by - DerekSaw on 4/9/00 10:20:03 AM

##### Share on other sites
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

##### Share on other sites
I should have seen that. . . .

Thanks

##### Share on other sites
defines are always messy. this mistake happens to me all the time although i should know better

const int ciZoomLevel = 1;

##### Share on other sites
Perhaps constants *are* better. Except for one thing...they take up space just like variables, whereas #defines just replace text before compiling.

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

• ### Forum Statistics

• Total Topics
628312
• Total Posts
2981996

• 9
• 9
• 13
• 11
• 13