• Advertisement

Archived

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

Is this legal?

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is this legal: #define sumthin cause when I try to do in in Linux, gcc acts like sumthin is actually something, like a variable or something, instead of nothing, like the way Visual C++ seems to treat it. What I''m using it for is in Windows, sumthin is something else, and in Linux, sumthin is nothing, cause I don''t need it.

Share this post


Link to post
Share on other sites
Advertisement
When you use a define like that this is what happens:

  
#define MACRO "Test"

//later you do this...

char phrase[] = MACRO;

//What the compiler does is substitute the define

//with what is after it, so you get this:

//char phrase[] = "Test";



That make sense?


Landsknecht

Share this post


Link to post
Share on other sites
Yes, it is legal, the code police will not come to your door.
  
#define FOO

void main()
{
FOO = 5;
int a = FOO;
};
yields, with gcc 2.96

foo.cc: In function `int main (...)'':
foo.cc:5: parse error before `=''
foo.cc:6: parse error before `;''


Which is what is expected (missing token).
The preprocessor acts _before_ the compiler.
If you want to see by yourself, run gcc -E foo.cc, which yields (on stdout)

# 3 "foo.cc"
void main()
{
= 5;
int a = ;
};


Just as expected.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
If you just do

#define sumthin

It doesn''t expand to anything worth noting but it''s been #defined for the purposes of later #ifdefs, #ifndefs and whatever else.

Share this post


Link to post
Share on other sites

  • Advertisement