/****************************
*功能:输出源文件的标题和当前执行
* 行的行数
*目的:知识学习
****************************/
#include<iostream>
using namespace std;
void main()
{
int line=__LINE__;//注意:LINE前后分别是两个下划线“-”(半角状态下)
char*file=__FILE__;
cout<<"file name is "<<(file)<<",line is "<<line<<endl;
}
Table 1.1 ANSI Predefined Macros
Macro | Description |
__DATE__ | The compilation date of the current source file. The date is a string literal of the formMmm dd yyyy. The month nameMmm is the same as for dates generated by the library functionasctime declared in TIME.H. |
__FILE__ | The name of the current source file. __FILE__ expands to a string surrounded by double quotation marks. |
__LINE__ | The line number in the current source file. The line number is a decimal integer constant. It can be altered with a#line directive. |
__STDC__ | Indicates full conformance with the ANSI C standard. Defined as the integer constant 1 only if the /Za compiler option is given and you are not compiling C++ code; otherwise is undefined. |
__TIME__ | The most recent compilation time of the current source file. The time is a string literal of the formhh:mm:ss. |
__TIMESTAMP__ | The date and time of the last modification of the current source file, expressed as a string literal in the formDdd Mmm Date hh:mm:ss yyyy, whereDdd is the abbreviated day of the week andDate is an integer from 1 to 31. |
#line
命令# line改变_LINE_ 与_ F I L E _的内容,它们是在编译程序中预先定义的标识符。
命令的基本形式如下:
# line number["filename"]
其中的数字为任何正整数,可选的文件名为任意有效文件标识符。行号为源程序中当前行号,文件名为源文件的名字。命令# line主要用于调试及其它特殊应用。
函数“line”举例
例如,下面说明行计数从1 0 0开始;printf( ) 语句显示数1 0 2,因为它是语句#line 100后的第3行。
#line 100 /* 初始化行计数器* /
main ( ) /* 行号100 是从定以后的下一行开始计数的*/
{ /* 行号101 */
p r i n t f ( " % d \ n " ,_ line _ ) ; /* 行号102 */
}