c语言中的 号和 号的作用,C语言中的#号用法!(#, #@, ##)

C/C++ Preprocessor Reference

Preprocessor Operators

Four preprocessor-specific operators are used in the context of the #define directive (see the following list for a summary of each). The stringizing, charizing, and token-pasting operators are discussed in the next three sections. For information on the defined operator, see The #if, #elif, #else, and #endif Directives.

Operator

Action

Causes the corresponding actual argument to be enclosed in double quotation marks

Causes the corresponding argument to be enclosed in single quotation marks and to be treated as a character (Microsoft Specific)

Allows tokens used as actual arguments to be concatenated to form other tokens

Simplifies the writing of compound expressions in certain macro directives

C/C++ Preprocessor Reference

Stringizing Operator (#)

The number-sign or "stringizing" operator (#) converts macro parameters (after expansion) to string constants. It is used only with macros that take arguments. If it precedes a formal parameter in the macro definition, the actual argument passed by the macro invocation is enclosed in quotation marks and treated as a string literal. The string literal then replaces each occurrence of a combination of the stringizing operator and formal parameter within the macro definition.

White space preceding the first token of the actual argument and following the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.

Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark (") or backslash (\) character), the necessary escape backslash is automatically inserted before the character. The following example shows a macro definition that includes the stringizing operator and a main function that invokes the macro:#define stringer( x ) printf( #x "\n" )

int main()

{

stringer( In quotes in the printf function call\n );

stringer( "In quotes when printed to the screen"\n );

stringer( "This: \" prints an escaped double quote" );

}

Such invocations would be expanded during preprocessing, producing the following code:int main()

{

printf( "In quotes in the printf function call\n" "\n" );

printf( "\"In quotes when printed to the screen\"\n" "\n" );

printf( "\"This: \\\" prints an escaped double quote\"" "\n" );

}

When the program is run, screen output for each line is as follows:In quotes in the printf function call

"In quotes when printed to the screen"

"This: \" prints an escaped double quotation mark"

Microsoft Specific

The Microsoft C (versions 6.0 and earlier) extension to the ANSI C standard that previously expanded macro formal arguments appearing inside string literals and character constants is no longer supported. Code that relied on this extension should be rewritten using the stringizing (#) operator.

END Microsoft Specific

C/C++ Preprocessor Reference

Charizing Operator (#@)

Microsoft Specific

The charizing operator can be used only with arguments of macros. If #@ precedes a formal parameter in the definition of the macro, the actual argument is enclosed in single quotation marks and treated as a character when the macro is expanded. For example:#define makechar(x) #@x

causes the statementa = makechar(b);

to be expanded toa = 'b';

The single-quotation character cannot be used with the charizing operator.

END Microsoft Specific

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值