gcc编译器预定义宏(__FILE__,__FUNCTION__,__LINE__)用于输出跟踪代码,定位代码行

__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
发布了62 篇原创文章 · 获赞 28 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/native_lee/article/details/105382908