__FILE__
,__FUNCTION__
,__LINE__
使用案例如下,在编译时编译器会替换其值。用于更正
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
char * lang = getenv("LANG"); // 获取本程序运行的语言环境
if (NULL == lang)
{
return -1;
}
puts(lang);
char * a = getenv("a");
if (a == NULL)
{
printf("file:%s, %s:(line)%d\n", __FILE__, __FUNCTION__, __LINE__);
setenv("a", "345", 0);
}
else
{
puts(a);
setenv("a", "345", 1); // 第三个参数为1表示存在就替换, 为0表示,存在就算了
}
a = getenv("a");
puts(a);
unsetenv("a"); // 删除本程序的a的环境变量, 对父进程没有影响
a = getenv("a");
if (a == NULL)
{
printf("file:%s, %s:(line)%d\n", __FILE__, __FUNCTION__, __LINE__);
}
return 0;
}
结果如下
lmz@lmz-X280:~/桌面/code/c/test$ gcc getenv.c -o getenv
lmz@lmz-X280:~/桌面/code/c/test$ ./getenv
zh_CN.UTF-8
file:getenv.c, main:(line)15
345
file:getenv.c, main:(line)30