Sams Teach Yourself C++ in 21 Days

(singke) #1
What’s Next 759

21


which evaluates to
( (12) * (12) * (12) )
which, in turn, evaluates to 1728. THREE(5+7), however, evaluates to
5 + 7 * 5 + 7 * 5 + 7
Because multiplication has a higher precedence than addition, this becomes
5 + (7 * 5) + (7 * 5) + 7
which evaluates to
5 + (35) + (35) + 7
which finally evaluates to 82. As you can see, without the parenthesis, an error occurs—
three of 5+7 is really 36!

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 into one.

Stringizing ......................................................................................................


The stringizing operator puts quotes around any characters following the operator, up to
the next whitespace. Thus, if you write
#define WRITESTRING(x) cout << #x
and then call
WRITESTRING(This is a string);
the precompiler turns it into
cout << “This is a string”;
Note that the string This is a stringis put into quotes, as required by cout.

Concatenation ................................................................................................


The concatenation operator allows you to bond more than one term into a new word. The
new word is actually a token that can be used as a class name, a variable name, an offset
into an array, or anywhere else a series of letters might appear.
Free download pdf