Function-like Macros

Macros created by the #define directive are named snippets of code. For constants, the name reflects a literal value. On a more complex level, you can also assign actions (executed statements or logical chunks) to a macro, creating a function-like macro:

#define ALERT printf("Danger!")

With this directive, uses of ALERT in your C code will be replaced with that printf() statement.

As always, there is a catch here: C preprocessor directives must be defined on a single line. Once a new line is encountered, the old directive's definition is over. When the information for a directive won't fit on a single line (which is most likely to happen with function-like macros), you can break up the line by inserting a space followed by a backslash character () and continuing the information on the next line. When the preprocessor encounters this, the backslash character and any white space at the beginning of the following line are removed and the lines are joined together before the directive is processed. For example:

#define BIG_ALERT printf("*** This 
will get your attention! ***");

Function-like macros behave like functions, except that, when they're kept compact, they can execute much more quickly than a function.

To create a function-like macro

1.
Create a new text file in your IDE or text editor.

2.
Begin with your standard comments and the #include directive (Script 8.3):

/* newline.c - Script 8.3 */
#include <stdio.h>

Script 8.3. This function-like macro will print the newline character each time the C code refers to NL.


3.
Add a 72 directive to create a function-like macro:

#define NL printf("
")

This line creates a macro that associates the code printf(" ") with the abbreviation NL. The code itself will print a newline character. Note that there's no semicolon after the function call here, because that's the C syntax and this is a C preprocessor realm.

4.
Define the main function:

int main (void) {

5.
Print some text and then reference the NL macro:

printf ("This is some text.");
NL;

Before compilation occurs, the C preprocessor will replace NL with printf(" "). Thus, the second line here is the equivalent of having

printf("
");

in your C code.

6.
Print some more text along with calls to the NL macro:

printf ("This will show up on the
 next line.");
NL;
NL;
printf ("This will show up two lines
 later.");

As you can tell, the text itself is rather silly, but the output of this application will show how the C preprocessor inserted the printf(" ") calls as if they were hardcoded into the C.

7.
Complete the main function:

    getchar();
    return 0;
}

8.
Save the file as newline.c, compile, and debug.

9.
Run the executable (Figure 8.7).

Figure 8.7. This application uses function-like macros to create newlines.


10.
If you want, use the compiler to view the post-processed code (Figure 8.8).

Figure 8.8. The post-processed version of the code demonstrates how references to NL are replaced with printf(" ").


If you've forgotten the syntax for doing this, review the first part of this chapter.

✓ Tips

  • Because the backslash line continuation will read through any white space on the following line, you can make extended macros more legible by indenting subsequent code:

    #define BIG_ALERT printf("*** This 
         will get your attention! ***");
    
  • As with all macros, keep in mind that function-like macros are a simple replacement in your C code. No syntax checking is performed on the code until compilation. If you have errors in the syntax of a macro, the compiler may report a syntax error on a line that appears to be perfectly fine.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset