1. Define object macros
Standard C language preprocessing requires the definition of certain object macros. The name of each predefined macro begins and ends with an underscore character or two. These predefined macros cannot be undefined (#undef) or redefined by the programmer. The following predefined macro table was copied by me.
- __LINE__ : The line number of the current program line, expressed as a decimal integer constant
- __FILE__ : the name of the current source file, representing a string constant
- __DATE__ : The converted calendar date, expressed as a string constant in the form of Mmm dd yyyy, Mmm is generated by asctime.
- __TIME__ : Conversion time, representing a string constant in the form of "hh:mm:ss", which is generated by asctime. (asctime seems to refer to a function)
- __STDC__ : bit-decimal integer constant when the editor is implemented for ISO compatibility
- __STDC_VERSION__ : How to implement composite C89 integral 1, the value of this macro is 19940SL; if the implementation complies with C99, the value of this macro is 199901L; otherwise the value is undefined
- __STDC_EOBTED__ : (C99) 1 when implemented as a host implementation, 0 when implemented as an independent implementation
- __STDC_IEC_559__ : (C99) The floating point number is defined as 1 when implementing the composite IBC 60559 standard, otherwise the value is undefined
- __STDC_IEC_559_COMPLEX__: (C99) When the complex operation implements the compound IBC 60559 standard, it is defined as 1, otherwise the value is undefined
- __STDC_ISO_10646__ : (C99) is defined as a long integer constant, yyyymmL represents a wchar_t value compounding the ISO 10646 standard and its revisions for the specified year and month, otherwise the value is undefined
__cplusplus is also defined in C++
__FILE__ , __LINE__ and __DATE__ in C language are all in the header file #include<stdio.h>
other instructions:
If the compiler is not standard, it may support only a few of the above macro names, or none at all. Remember that the compiler may also provide other predefined macro names.
__LINE__ and __FILE__ macro instructions, the #line instruction can change its value, simply speaking, when compiling, they contain the current line number and file name of the program.
The meaning of the __STDC__ macroinstruction is defined at compile time. Generally speaking, if __STDC__ is defined, the compiler will only accept standard C/C++ code that does not contain any non-standard extensions. If the implementation is standard, the macro __STDC__ contains the decimal constant 1. If it contains any other number, the implementation is non-standard.
__cplusplus Compilers consistent with Standard C++ define it as a value containing at least 6. Compilers that do not conform to standard C++ will use values with 5 bits or less.
#include <stdio.h>
int main(void)
{
printf("%d\n",__LINE__);
printf("%d\n",__LINE__);
printf("%d\n",__LINE__);
printf("%s\n",__FILE__);
printf("%s\n",__DATE__);
printf("%s\n",__TIME__);
return 0;
}
2. Print log
Sometimes, it is necessary to print the process or thread ID in the Log to debug and find problems.
get process id
#include <unistd.h>
pid_t getpid(void);
get thread id
// 方法1
#include <sys/syscall.h>
syscall(__NR_gettid)
// 方法2
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)
// 方法3
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
When there is only one thread, the pid is returned.