首页 > 代码库 > C语言预处理命令
C语言预处理命令
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, see Predefined 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.See The #if, #elif, #else, and #endif Directives for 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
http://msdn.microsoft.com/zh-cn/library/3sxhs2ty.aspx