Public Group

# commenting with define

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

## Recommended Posts

Hi, everyone, I started teaching myself c++ a couple weeks ago. Today I had a question I figured I might as well try posting here. I was under the impression that when you use #define, it blindly replaces all occurrences before compiling beings, so I did
#define FOO //
FOO x++;


It compiled fine, but for some reason, the statements executed, even though they were supposed to be commented out. What did I do wrong? I hope this isn't a stupid question.

##### Share on other sites
Comments are stripped before #defines are processed.

I hope you weren't planning to do that for real, though....

##### Share on other sites
The preprocessor understands, and therefore ignores, comments. FOO is defined as nothing.

##### Share on other sites
Quote:
 Original post by CodekaI hope you weren't planning to do that for real, though....

It was the best way I could think of for enabling and disabling many statements easily

##### Share on other sites
Quote:
 Original post by StoryyellerIt was the best way I could think of for enabling and disabling many statements easily

Then you'll want to learn about the other preprocessor directives that are for exactly that purpose.

##### Share on other sites
Quote:
Original post by Storyyeller
Quote:
 Original post by CodekaI hope you weren't planning to do that for real, though....

It was the best way I could think of for enabling and disabling many statements easily

Why not just use // in front or the line? It's less characters than FOO anyway.
Or for a longer statement, just use /* at the start and */ at the end of what you want to comment out.

##### Share on other sites
Quote:
 Original post by ChadwellWhy not just use // in front or the line? It's less characters than FOO anyway.
Because it wouldn't allow him to conditionally disable a large number of nonconsecutive lines.

BTW, Storyyeller, there's a hackish and ugly way of doing this:

#define FOO if(0)

But yeah, #ifndef is your friend here.

##### Share on other sites
Quote:
 Original post by CodekaComments are stripped before #defines are processed.

Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.

##### Share on other sites
Quote:
Original post by scratt
Quote:
 Original post by CodekaComments are stripped before #defines are processed.

Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.

lol, if you comment the defines, then they will get stripped thus will not be compiled.

pass me some of those beers, tnx. ;)

##### Share on other sites
Quote:
Original post by owl
Quote:
Original post by scratt
Quote:
 Original post by CodekaComments are stripped before #defines are processed.

Is that specific to certain compilers, or standard?
Perhaps I have misunderstood, but I regularly comment out defines in XCode, using gcc, and they are not compiled.

lol, if you comment the defines, then they will get stripped thus will not be compiled.

pass me some of those beers, tnx. ;)

lol
I guess i read it the wrong way around.
i.e. Misinterpreting comments are stripped, as meaning // is stripped, before #define is processed, thus exposing #define.
Which it obviously does not.

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999977
×