C标准预定义宏__FILE__和f__LINE__

733阅读 0评论2012-10-14 momser
分类:

The standard predefined macros are specified by the C and/or C++language standards, so they are available with all compilers thatimplement those standards. Older compilers may not provide all ofthem. Their names all start with double underscores.
__FILE__
This macro expands to the name of the current input file, in the form ofa C string constant.This is the path by which the preprocessor openedthe file, not the short name specified in `#include' or as theinput file name argument. For example,"/usr/local/include/myheader.h" is a possible expansion of thismacro.
__LINE__
This macro expands to the current input line number, in the form of adecimal integer constant. While we call it a predefined macro, it'sa pretty strange macro, since its "definition" changes with eachnew line of source code.
__FILE__ and __LINE__ are useful in generating an errormessage to report an inconsistency detected by the program; the messagecan state the source line at which the inconsistency was detected. Forexample,
 fprintf (stderr, "Internal error: negative string length %d at %s, line %d.", length, __FILE__, __LINE__);
An `#include' directive changes the expansions of __FILE__and __LINE__ to correspond to the included file. At the end ofthat file, when processing resumes on the input file that containedthe `#include' directive, the expansions of __FILE__ and__LINE__ revert to the values they had before the`#include' (but __LINE__ is then incremented by one asprocessing moves to the line after the `#include').
A `#line' directive changes __LINE__, and may change__FILE__ as well. See section 6. Line Control.
C99 introduces __func__, and GCC has provided __FUNCTION__for a long time. Both of these are strings containing the name of thecurrent function (there are slight semantic differences; see the GCCmanual). Neither of them is a macro; the preprocessor does not know thename of the current function. They tend to be useful in conjunctionwith __FILE__ and __LINE__, though.
__DATE__
This macro expands to a string constant that describes the date on whichthe preprocessor is being run. The string constant contains elevencharacters and looks like "Feb 12 1996". If the day of themonth is less than 10, it is padded with a space on the left.
__TIME__
This macro expands to a string constant that describes the time atwhich the preprocessor is being run. The string constant containseight characters and looks like "23:59:01".
__STDC__
In normal operation, this macro expands to the constant 1, to signifythat this compiler conforms to ISO Standard C. If GNU CPP is used witha compiler other than GCC, this is not necessarily true; however, thepreprocessor always conforms to the standard, unless the`-traditional' option is used.
This macro is not defined if the `-traditional' option is used.
On some hosts, the system compiler uses a different convention, where__STDC__ is normally 0, but is 1 if the user specifies strictconformance to the C Standard. GNU CPP follows the host convention whenprocessing system header files, but when processing user files__STDC__ is always 1. This has been reported to cause problems;for instance, some versions of Solaris provide X Windows headers thatexpect __STDC__ to be either undefined or 1. See section 12. Invocation.
__STDC_VERSION__
This macro expands to the C Standard's version number, a long integerconstant of the form yyyymmL where yyyy andmm are the year and month of the Standard version. This signifieswhich version of the C Standard the compiler conforms to. Like__STDC__, this is not necessarily accurate for the entireimplementation, unless GNU CPP is being used with GCC.
The value 199409L signifies the 1989 C standard as amended in1994, which is the current default; the value 199901L signifiesthe 1999 revision of the C standard. Support for the 1999 revision isnot yet complete.
This macro is not defined if the `-traditional' option is used, norwhen compiling C++ or Objective-C.
__STDC_HOSTED__
This macro is defined, with value 1, if the compiler's target is ahosted environment. A hosted environment has the completefacilities of the standard C library available.
__cplusplus
This macro is defined when the C++ compiler is in use. You can use__cplusplus to test whether a header is compiled by a C compileror a C++ compiler. This macro is similar to __STDC_VERSION__, inthat it expands to a version number. A fully conforming implementationof the 1998 C++ standard will define this macro to 199711L. TheGNU C++ compiler is not yet fully conforming, so it uses 1instead. We hope to complete our implementation in the near future.

#include

int main(int argc, char *argv[])
{
    printf("%s\n", __FILE__);
    printf("%s\n", __FUNCTION__);
    printf("%d\n", __LINE__);
    printf("%s\n", __DATE__);
    printf("%d\n", __STDC__);
    printf("%s\n", __TIME__);
}
上一篇:__attribute__
下一篇:linux内核编译环境配置