String Manipulation

The preprocessor provides two special operators for manipulating strings in macros. The stringizing operator (#) substitutes a quoted string for whatever follows the stringizing operator. The concatenation operator (##) bonds two strings together into one.

Stringizing

The stringizing operator(#) puts quotes around any characters following the operator, up to the next white space. So, if you write:

#define WRITESTRING(x) cout << #x

and then call

WRITESTRING(This is a string);

the precompiler will turn it into this:

cout << "This is a string";

Note that the string This is a string is put into quotes, as required by cout.

Concatenation

The concatenation operator (##) enables you to bond together more than one term into ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.