http://gcc.gnu.org/onlinedocs/cpp/Predefined-Macros.html
Several object-like macros are predefined; you use them without supplying their definitions. They fall into three classes: standard, common, and system-specific.
In C++, there is a fourth category, the named operators. They act like predefined macros, but you cannot undefine them.
有用的實例:打印當前的文件名和行號
__FILE__
- This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in ‘#include’ or as the input file name argument. For example,
"/usr/local/include/myheader.h"
is a possible expansion of this macro. __LINE__
- This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, it's a pretty strange macro, since its “definition” changes with each new line of source code.
__FILE__
and __LINE__
are useful in generating an error message to report an inconsistency detected by the program; the message can state the source line at which the inconsistency was
detected. For example,
fprintf (stderr, "Internal error: " "negative string length " "%d at %s, line %d.", length, __FILE__, __LINE__);