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.

462 Appendix A C Language Summary<br />

If a def<strong>in</strong>ition requires more than one l<strong>in</strong>e, each l<strong>in</strong>e to be cont<strong>in</strong>ued must be ended<br />

with a backslash character. After a name has been def<strong>in</strong>ed, it can be used subsequently<br />

anywhere <strong>in</strong> the file.<br />

The # operator is permitted <strong>in</strong> #def<strong>in</strong>e directives that take arguments. It is followed<br />

by the name of an argument to the macro.The preprocessor puts double quotation<br />

marks around the actual value passed to the macro when it’s <strong>in</strong>voked.That is, it turns it<br />

<strong>in</strong>to a character str<strong>in</strong>g. For example, the def<strong>in</strong>ition<br />

#def<strong>in</strong>e pr<strong>in</strong>t<strong>in</strong>t(x) pr<strong>in</strong>tf (# x " = %d\n", x)<br />

with the call<br />

pr<strong>in</strong>t<strong>in</strong>t (count);<br />

is expanded by the preprocessor <strong>in</strong>to<br />

pr<strong>in</strong>tf ("count" " = %i\n", count);<br />

or, equivalently,<br />

pr<strong>in</strong>tf ("count = %i\n", count);<br />

The preprocessor puts a \ character <strong>in</strong> front of any " or \ characters when perform<strong>in</strong>g<br />

this str<strong>in</strong>giz<strong>in</strong>g operation. So, with the def<strong>in</strong>ition<br />

#def<strong>in</strong>e str(x) # x<br />

the call<br />

str (The str<strong>in</strong>g "\t" conta<strong>in</strong>s a tab)<br />

expands to<br />

"The str<strong>in</strong>g \"\\t\" conta<strong>in</strong>s a tab"<br />

The ## operator is also allowed <strong>in</strong> #def<strong>in</strong>e directives that take arguments. It is preceded<br />

(or followed) by the name of an argument to the macro.The preprocessor takes the value<br />

that is passed when the macro is <strong>in</strong>voked and creates a s<strong>in</strong>gle token from the argument<br />

to the macro and the token that follows (or precedes) it. For example, the macro<br />

def<strong>in</strong>ition<br />

#def<strong>in</strong>e pr<strong>in</strong>tx(n) pr<strong>in</strong>tf ("%i\n", x ## n );<br />

with the call<br />

pr<strong>in</strong>tx (5)<br />

produces<br />

pr<strong>in</strong>tf ("%i\n", x5);<br />

The def<strong>in</strong>ition<br />

#def<strong>in</strong>e pr<strong>in</strong>tx(n) pr<strong>in</strong>tf ("x" # n " = %i\n", x ## n );<br />

with the call<br />

pr<strong>in</strong>tx(10)

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

Saved successfully!

Ooh no, something went wrong!