Internet & Web Application

Token # และ ## ใน Macro of C-language ที่ลิมประจำ

8.1.3 Conversions to String Literals #The # preprocessor operator is used to convert the argument that follows it to a string literal. The preprocessor operator # can be used only in a function-like macro definition. For example:#include #define PRid printf”The value of ” #id ” is %d\n”, idmain{ int i = 10; PRi;}The output produced is:The value of i is 10The macro call expands in the following steps:/1/ printf”The value of ” #id ” is %d\n”, id/2/ printf”The value of ” “i” ” is %d\n”, 10/3/ printf”The value of i is %d\n”, 10The unary # operator produces a string from its operand. This example also uses the fact that adjacent string literals are concatenated. If the operand to # contains double quotes or escape sequences, they are also expanded. For example:#include #define Marg printf#arg ” is %s\n”, argmain{ M”a\nb\tc”;}The macro call expands using the following steps:/1/ printf#arg ” is %s\n”, arg/2/ printf”\”a\\nb\\tc\”” ” is %s\n”, “a\nb\tc”;/3/ printf”\”a\\nb\\tc\” is %s\n”, “a\nb\tc”;8.1.4 Token Concatenation##The ## preprocessor operator is used to concatenate two tokens into a third valid token, as in the following example:#define gluea,b a ## bmain{int wholenum = 5000;printf”%d”, gluewhole,num;}The preprocessor converts the line printf”%d”, gluewhole,num; into printf”%d”, wholenum; , and when executed, the program prints 5000. If the result is not a valid token, an error occurs when the tokens are concatenated.

via Macro Definition #define and #undef.

Advertisements
มาตรฐาน

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s