The preprocessor is old, primitive, and strong. But you need to understand its strengths to use it effectively. It adds capabilities that the C++ compiler can’t come close to duplicating on its own.

All the preprocessor directives begin with the hash character, #, and do not use a semicolon at the end. The end of a directive is just the end of the line. You can extend a preprocessor directive to other lines by ending each line to be extended with a backslash character, \.

The using statement is the most common preprocessor directive and might look like this:

You’ll also often find conditional sections like this:

A very common pragma directive that allows you to specify that an include file should be processed only once no matter how many times it gets included in a particular compilation unit is:

And you can also use preprocessor directives to define elaborate replacement text like this:

Using this macro like this:

Would be equivalent to writing the following:

This macro not only replaced MYCLASS with the content of the macro but the ## symbols caused the text passed as name and id to be concatenated to form the class name.


What's on your mind?

On a scale of 0 to 10, how likely are you to refer us to a friend?