18-预定义宏
16.6.4 预定义宏
C标准规定了一些预定义宏,如表16.1所列。
| 宏 | 含义 |
| :----- | :----- | :----- | :----- |
| _ _DATE_ _
| 预处理的日期( "Mmm dd yyyy"
形式的字符串字面量,如 Nov 23 2013
) |
| _ _FILE_ _
| 表示当前源代码文件名的字符串字面量 |
| _ _LINE_ _
| 表示当前源代码文件中行号的整型常量 |
| _ _STDC_ _
| 设置为 1
时,表明实现遵循 C
标准 |
| _ _STDC_HOSTED_ _
| 本机环境设置为 1
;否则设置为 0
|
| _ _STDC_VERSION_ _
| 支持 C99
标准,设置为 199901L
;支持 C11
标准,设置为 201112L
|
| _ _TIME_ _
| 翻译代码的时间,格式为“ hh:mm:ss
” |
C99标准提供一个名为 _ _func_ _
的预定义标识符,它展开为一个代表函数名的字符串(该函数包含该标识符)。那么, _ _func_ _
必须具有函数作用域,而从本质上看宏具有文件作用域。因此, _ _func_ _
是C语言的预定义标识符,而不是预定义宏。
程序清单16.12中使用了一些预定义宏和预定义标识符。注意,其中一些是C99新增的,所以不支持C99的编译器可能无法识别它们。如果使用GCC,必须设置 -std=c99
或 -std=c11
。
程序清单16.12 predef.c
程序
// predef.c -- 预定义宏和预定义标识符
#include <stdio.h>
void why_me();
int main()
{
printf("The file is %s.\n", _ _FILE_ _);
printf("The date is %s.\n", _ _DATE_ _);
printf("The time is %s.\n", _ _TIME_ _);
printf("The version is %ld.\n", _ _STDC_VERSION_ _);
printf("This is line %d.\n", _ _LINE_ _);
printf("This function is %s\n", _ _func_ _);
why_me();
return 0;
}
void why_me()
{
printf("This function is %s\n", _ _func_ _);
printf("This is line %d.\n", _ _LINE_ _);
}
下面是该程序的输出:
The file is predef.c.
The date is Sep 23 2013.
The time is 22:01:09.
The version is 201112.
This is line 11.
This function is main
This function is why_me
This is line 21.