The macro definition described below can be used in code without reference to other header files. The specific role has provided us with a lot of convenience. The descriptions are as follows:
1 -- _ file __,__ line __,__ function _ or _ FUNC __
_ File __: name of the source file of the current program line, supported by standard C. This macro is treated as a string;
_ Line __: the row number in the source file where the current program row is located. Supported by standard C. This macro is treated as an integer;
_ Function _ or _ FUNC __: name of the function to which the current program row belongs. Supported by c99 (for example, not supported by VC ++ 6.0). This macro is treated as a string;
Combined with the log printing function, this information will help debugging. Simple usage:
// Save the program as test. CPP # include <iostream> using namespace STD; int main (INT argc, char * argv []) {printf ("file: % S | line: % d | function: % S | % s \ n ", _ file __, _ line __, _ FUNCTION __, _ FUNC _); Return 0 ;}
Execute the above program to print: file: Test. cpp | line: 8 | function: Main | main
In particular, according to the reference information about _ FUNC _, __func _ Is Not A Macro. It is an array of constant characters that are implicitly declared: static const char _ FUNC _ [] = "function-name ".
2 -- _ date __,__ time __
_ Date __: current file compilation date. Format: Mmm: DD: yyyy. This macro is treated as a string.
_ Time __: current file Compilation Time in the format of HH: mm: Ss. This macro is treated as a string.
If this macro is used to compile the source file, the program can print the Compilation Time to differentiate different versions. The simple method is as follows:
#include <iostream>using namespace std;int main(int argc, char *argv[]){printf("DATE:%s|TIME:%s\n", __DATE__, __TIME__);getchar();return 0;}
Execute the above program to print: Date: Oct 20 2010 | time: 23: 33: 24. If you do not re-compile the program, it will be the current time rather than the current time of the system for each execution of the program. This time should be understood differently.
3 -- # Line
# Line is used to reset the row number and file name specified by _ line _ and _ file _ macros. For example, we have such a test program:
// Save the program as test. CPP # include <iostream> using namespace STD; int main (INT argc, char * argv []) {# line 100 "Baidu. CPP "printf (" file: % S | line: % d \ n ", _ file __, _ line _); Return 0 ;}
Comment out 8th lines of code and print the program: file: Test. cpp | line: 9.
Without comments of Line 1 code, the program prints: file: Baidu. cpp | line: 8th
Visible: # Line specifies the starting line number and source file name of the downstream code, and the scope ends at the end of the file or # line again.
4 -- _ gunc __
_ Gunc __is a preference of the GCC compiler, indicating the current version compiled by gnuc. The value of _ gnuc _ indicates the GCC version. You can use this macro to compile the code for a specific GCC version.
6 -- macro-defined "#" and "#" Usage
"#": When the macro parameter is replaced, convert the macro parameter following it into a string with quotation marks, for example:
#define STR(s) #sint main(){std::string str = STR(abcdefg);return 0;}
When the C compiler pre-processes the code, the first line is actually translated into: STD: String STR = "abcdefg ";
"##": Connect the two macro parameters before and after the symbol, for example:
#define PRINT(PRINT_FUNNAME, VAR) print_##PRINT_FUNNAME(VAR)int main(){PRINT(common, 5);return 0;}
When the C compiler pre-processes the code, 5th lines are actually translated into: print_common (5 );
Let's take a look at the example of comprehensive application:
//#include <iostream>#define PRINT(fun, name, var) print_##fun(#name, var)void print_common(const std::string & name, int var){std::cout << name << ":" << var << std::endl;}void print_tofile(const std::string & name, int var){char sz_temp[1024];memset(sz_temp, 0, sizeof(sz_temp));snprintf(sz_temp, sizeof(sz_temp), "%s:%d", name.c_str(), var);FILE * fp = fopen("./log.log", "w");fwrite(sz_temp, 1, strlen(sz_temp), fp);fclose(fp);}int main(){PRINT(common, age, 5);PRINT(tofile, age, 5);return 0;}
This Code indicates that, in the main function, a unified call entry "print" macro function is used to print the variable name and value to different places by specifying different function names, for example, in a screen or file.
We use the G ++-e Marco. cpp preprocessing command to view the pre-processed source files:
# 1 "marco.cpp"# 1 "<built-in>"# 1 "<command line>"# 1 "marco.cpp"void print_common(const std::string & name, int var){std::cout << name << ":" << var << std::endl;}void print_tofile(const std::string & name, int var){char sz_temp[1024];memset(sz_temp, 0, sizeof(sz_temp));snprintf(sz_temp, sizeof(sz_temp), "%s:%d", name.c_str(), var);FILE * fp = fopen("./log.log", "w");fwrite(sz_temp, 1, strlen(sz_temp), fp);fclose(fp);}int main(){print_common("age", 5);print_tofile("age", 5);return 0;}7 -- Reference Document & extended reading
1/http://blog.csdn.net/chief1985/archive/2008/07/25/2711526.aspx
2/http://lukas06.blog.sohu.com/95352179.html