请教#line的用途

leozheng 2003-08-18 08:37:44
请问哪位知道#line的用途。多谢!
...全文
38 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
leozheng 2003-08-18
  • 打赏
  • 举报
回复
非常感谢两位!
fireseed 2003-08-18
  • 打赏
  • 举报
回复
C/C++ Preprocessor Reference

The #line DirectiveSee Also
Preprocessor Directives
The #line directive tells the preprocessor to change the compiler's internally stored line number and filename to a given line number and filename. The compiler uses the line number and 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.

#line digit-sequence "filename"opt
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 (" "). If filename 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 #line directive 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__ );}
See Also
Preprocessor Directives



--------------------------------------------------------------------------------

Send feedback to Microsoft

© 2001 Microsoft Corporation. All rights reserved.
aflyinghorse 2003-08-18
  • 打赏
  • 举报
回复
#line的语法如下:
#line number filename
例如:#line 30 a.h 其中,文件名a.h可以省略不写。
这条指令可以改变当前的行号和文件名,例如上面的这条预处理指令就可以改变当前的行号为30,文件名是a.h。初看起来似乎没有什么用,不过,他还是有点用的,那就是用在编译器的编写中,我们知道编译器对C++源码编译过程中会产生一些中间文件,通过这条指令,可以保证文件名是固定的,不会被这些中间文件代替,有利于进行分析。

http://www.csdn.net/develop/read_article.asp?id=19490

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧