Sams Teach Yourself C in 21 Days

(singke) #1
and you invoke it with the statement
OUT(Hello Mom);
it expands to this statement:
printf(“Hello Mom”);
The conversion performed by the stringizing operator takes special characters into
account. Thus, if a character in the argument normally requires an escape character, the #
operator inserts a backslash before the character. Continuing with the example, the invo-
cation
OUT(“Hello Mom”);
expands to
printf(“\”Hello Mom\””);
The#operator is demonstrated in Listing 21.4. First, you need to look at one other oper-
ator used in macros, the concatenation operator(##). This operator concatenates, or
joins, two strings in the macro expansion. It doesn’t include quotation marks or special
treatment of escape characters. Its main use is to create sequences of C source code. For
example, if you define and invoke a macro as
#define CHOP(x) func ## x
salad = CHOP(3)(q, w);
the macro invoked in the second line is expanded to
salad = func3 (q, w);
You can see that, by using the ##operator, you determine which function is called. You
have actually modified the C source code.
Listing 21.4 shows an example of one way to use the #operator.

LISTING21.4 preproc.c. Using the #operator in macro expansion
1: /* Demonstrates the # operator in macro expansion. */
2:
3: #include <stdio.h>
4:
5: #define OUT(x) printf(#x “ is equal to %d.\n”, x)
6:
7: int main( void )
8: {
9: int value = 123;
10: OUT(value);

604 Day 21

33 448201x-CH21 8/13/02 11:16 AM Page 604

Free download pdf