c语言 macro in compile time,C语言预处理命令

本文详细介绍了C/C++中的三个预处理器指令:#error用于在编译时发出错误并终止编译,#line用于改变源代码的行号和文件名,而#undef则用于取消宏定义。这些指令在代码调试和生成中起到关键作用,例如通知开发者程序不一致、控制条件编译等。
摘要由CSDN通过智能技术生成

1.#error Directive (C/C++)

The #error directive emits a user-specified error message at compile time and then terminates the compilation.

#error token-string

The error message that this directive emits includes the token-string parameter. The token-string parameter is not subject to macro expansion. This directive is most useful during preprocessing for notifying the developer of a program inconsistency or the violation of a constraint. The following example demonstrates error processing during preprocessing:

#if !defined(__cplusplus)

#error C++ compiler required.

#endif

2.#line Directive (C/C++)

The #line directive tells the preprocessor to change the compiler's internally stored line number and filename to a given line number and filename.

#line digit-sequence ["filename"]

The compiler uses the line number and optional filename to refer to errors that it finds during compilation. The line number usually refers to the current input line, and the filename refers to the current input file. The line number is incremented after each line is processed.

The digit-sequence value can be any integer constant. Macro replacement can be performed on the preprocessing tokens, but the result must evaluate to the correct syntax. The filename can be any combination of characters and must be enclosed in double quotation marks (" "). Iffilename is omitted, the previous filename remains unchanged.

You can alter the source line number and filename by writing a #line directive. The translator uses the line number and filename to determine the values of the predefined macros __FILE__ and __LINE__. You can use these macros to insert self-descriptive error messages into the program text. For more information on these predefined macros, seePredefined Macros.

The __FILE__ macro expands to a string whose contents are the filename, surrounded by double quotation marks (" ").

If you change the line number and filename, the compiler ignores the previous values and continues processing with the new values. The #linedirective is typically used by program generators to cause error messages to refer to the original source file instead of to the generated program.

The following examples illustrate #line and the __LINE__ and __FILE__ macros.

In this statement, the internally stored line number is set to 151 and the filename is changed to copy.c.

#line 151 "copy.c"

In this example, the macro ASSERT uses the predefined macros __LINE__ and __FILE__ to print an error message about the source file if a given "assertion" is not true.

#define ASSERT(cond)

if( !(cond) )\

{printf("assertion error line %d, file(%s)\n", \

__LINE__, __FILE__ );}

3.#undef Directive (C/C++)

Removes (undefines) a name previously created with #define.

#undef identifier

The #undef directive removes the current definition of identifier. Consequently, subsequent occurrences of identifier are ignored by the preprocessor. To remove a macro definition using #undef, give only the macro identifier; do not give a parameter list.

You can also apply the #undef directive to an identifier that has no previous definition. This ensures that the identifier is undefined. Macro replacement is not performed within #undef statements.

The #undef directive is typically paired with a #define directive to create a region in a source program in which an identifier has a special meaning. For example, a specific function of the source program can use manifest constants to define environment-specific values that do not affect the rest of the program. The #undef directive also works with the #if directive to control conditional compilation of the source program.SeeThe #if, #elif, #else, and #endif Directivesfor more information.

In the following example, the #undef directive removes definitions of a symbolic constant and a macro. Note that only the identifier of the macro is given.

#define WIDTH 80

#define ADD( X, Y ) ((X) + (Y)).

.

.#undef WIDTH

#undef ADD

Microsoft Specific

Macros can be undefined from the command line using the /U option, followed by the macro names to be undefined. The effect of issuing this command is equivalent to a sequence of #undef macro-name statements at the beginning of the file.

END Microsoft Specific

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值