预定义宏

预处理器

//__func__ (C99) is not a macro, but a string that every function
//has: the name of this function
const char *whereAmI(void) {
    return __func__;
}

const char *function = whereAmI();
//function is "whereAmI"

//__FILE__ and __LINE__: the file and the line being compiled.
//A message can tell where it comes from
#define LOG(message) printf("%s:%d: %s\n", __FILE__, __LINE__, message)

//#line sets both, as code generators do
#line 100 "demo.c"
int line = __LINE__;
const char *file = __FILE__;
LOG("started");
//line is 100, file is "demo.c"
//printed "demo.c:102: started"

//__DATE__ and __TIME__: when the program was compiled
const char *buildDate = __DATE__;
//buildDate is the date of the build, like "Sep 30 2026"
const char *buildTime = __TIME__;
//buildTime is the time of the build, like "14:05:09"

//the version of the standard: 199901 is C99, 201112 is C11,
//201710 is C17, 202311 is C23
long version = __STDC_VERSION__;
//version is 202311

printf("function is %s\n", function);
printf("line is %d\n", line);
printf("file is %s\n", file);
printf("buildDate is %s\n", buildDate);
printf("buildTime is %s\n", buildTime);
printf("version is %ld\n", version);