20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

9.0 The Preprocessor<br />

461<br />

Table A.7 Trigraph Sequences<br />

Trigraph<br />

Mean<strong>in</strong>g<br />

??> }<br />

??/ \<br />

??'<br />

^<br />

??! |<br />

??- ~<br />

9.2 Preprocessor Directives<br />

All preprocessor directives beg<strong>in</strong> with the character #, which must be the first nonwhitespace<br />

character on the l<strong>in</strong>e.The # can be optionally followed by one or more space or<br />

tab characters.<br />

9.2.1 The #def<strong>in</strong>e Directive<br />

The general format for declar<strong>in</strong>g the #def<strong>in</strong>e directive is as follows:<br />

#def<strong>in</strong>e name<br />

text<br />

This def<strong>in</strong>es the identifier name to the preprocessor and associates with it whatever text<br />

appears after the first blank space after name to the end of the l<strong>in</strong>e. Subsequent use of<br />

name <strong>in</strong> the program causes text to be substituted directly <strong>in</strong>to the program at that<br />

po<strong>in</strong>t.<br />

Another general format for declar<strong>in</strong>g the #def<strong>in</strong>e directive is as follows:<br />

#def<strong>in</strong>e name(param_1, param_2, ..., param_n) text<br />

The macro name is def<strong>in</strong>ed to take arguments as specified by param_1, param_2, ...,<br />

param_n, each of which is an identifier. Subsequent use of name <strong>in</strong> the program with an<br />

argument list causes text to be substituted directly <strong>in</strong>to the program at that po<strong>in</strong>t, with<br />

the arguments of the macro call replac<strong>in</strong>g all occurrences of the correspond<strong>in</strong>g parameters<br />

<strong>in</strong>side text.<br />

If the macro takes a variable number of arguments, three dots are used at the end of<br />

the argument list.The rema<strong>in</strong><strong>in</strong>g arguments <strong>in</strong> the list are collectively referenced <strong>in</strong> the<br />

macro def<strong>in</strong>ition by the special identifier __VA_ARGS__. As an example, the follow<strong>in</strong>g<br />

def<strong>in</strong>es a macro called myPr<strong>in</strong>tf to take a lead<strong>in</strong>g format str<strong>in</strong>g followed by a variable<br />

number of arguments:<br />

#def<strong>in</strong>e myPr<strong>in</strong>tf(...)<br />

pr<strong>in</strong>tf ("DEBUG: " __VA_ARGS__);<br />

Legitimate macro uses <strong>in</strong>cludes<br />

myPr<strong>in</strong>tf ("Hello world!\n");<br />

as well as<br />

myPr<strong>in</strong>tf ("i = %i, j = %i\n", i, j);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!