Tag Archives: C language advanced

The language of C__ FILE__ 、__ LINE__ And line

Original link:
 
http://hi.baidu.com/419836321/blog/item/fcf5ceec484681cfb31cb1f7.html
 
 
 
 

__FILE__ is used in C to indicate the filename of the source file in which the statement is written, for example (test.c) :

    #include < stdio.h> int main() { printf(“%s\n”,__FILE__); }

GCC compiler generates a.out. After execution, the output is:

test.c

Compiling results under Windows VC6.0 are as follows:

C :\ Documents and Settings \ Administrator \ Desktop \ Test.c

——————————————————————————————————————————————————————————————————– ————————————————————————–

__LINE__ in C is used to indicate the position of the statement in the source file. Examples are as follows:

    #include < stdio.h>

    main() { printf(“%d\n”,__LINE__); printf(“%d\n”,__LINE__); printf(“%d\n”,__LINE__); };

The program is compiled in Linux with GCC, in Windows VC6.0 under the compilation can pass, the execution results are:

7

8

9

__LINE__ can also be reset with the #line statement, for example:

    #include < stdio.h>

    # line 200 // specify the next row __LINE__ for 200 main () { printf (” % d \ n “, __LINE__); printf(“%d\n”,__LINE__); printf(“%d\n”,__LINE__); };

After compilation and execution, the output is:

202

203

204

——————————————————————————————————————————————————————————————————– —————————————————————————

GCC also supports __func__, which indicates the function, but this keyword is not supported in VC 6.0 under Windows, for example:

    #include < stdio.h> void main() { printf(“this is print by function %s\n”,__func__); }

The output result after compilation is

this is print by function main

Note # “line”, “__LINE__”, “a __FILE__” and “__func__” are case sensitive.