Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are comments always processed before the preprocessor? [duplicate]

/*
#define FOO
*/

#ifdef FOO
#define BAR "pirate"
#else
#define BAR "ninja"
#endif

int main() { printf(BAR); getchar(); }

In this code FOO is not defined (Visual Studio 2008). I assume that comments are processed first, then preprocessor, and then code. Are comments always processed before the preprocessor? Is this part of a standard?

like image 513
Nick Van Brunt Avatar asked Jan 05 '10 22:01

Nick Van Brunt


People also ask

Does C preprocessor remove comments?

Removing comments : It removes all the comments. A comment is written only for the humans to understand the code. So, it is obvious that they are of no use to a machine. So, preprocessor removes all of them as they are not required in the execution and won't be executed as well.

What happens in preprocessor stage?

Preprocessing is the first pass of any C compilation. It processes include-files, conditional compilation instructions and macros. Compilation is the second pass. It takes the output of the preprocessor, and the source code, and generates assembler source code.

How do you comment out a preprocessing line?

What is the best way to comment out a section of code that contains comments? The other way to put comments in your program is to use the // symbol. Everything from the // symbol to the end of the current line is omitted from the compiled version of the program.

How does the preprocessor work?

In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers.


2 Answers

I assume that comments are processed first, then preprocessor, and then code. Are comments always processed before the preprocessor?

Sort of -- part of the preprocessor's job is to remove comments. In this case, it doesn't care that you have the directive inside the comments; it's still removed just like any other comment.

like image 163
John Feminella Avatar answered Oct 11 '22 06:10

John Feminella


According to the C standard, there are 8 translation phases during translation (compilation) of a program. Each comment is replaced by a whitespace character in translation phase 3, whereas preprocessing directives are executed in phase 4.

like image 38
Alok Singhal Avatar answered Oct 11 '22 07:10

Alok Singhal