栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

linux C/C++ 宏定义

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux C/C++ 宏定义

一、用例环境

   输入: cat /etc/redhat-release

   系统环境:CentOS Linux release 7.5.1804 (Core) 

   测试文件名: test.c 

二、宏定义

2.1 __FILE__

  作用:表示当前源文件名,类型为字符串常量;

#include
int main()
{
    printf("%sn", __FILE__);
    return 0;
}
  输出为:

[root@bogon test-dir]# ./test
test.c


2.2 __LINE__

作用:代表当前程序行的行号,类型为十进制整数常量;

#include
int main()
{
    printf("%dn", __LINE__);
    printf("%dn", __LINE__);
    return 0;
}
输出为:

[root@bogon test-dir]# ./test
5
6


2.3 #line

语法:#line 行号 [“文件名”]

作用:将行号和文件名更改为指定的行号和文件名;

#include
#line 100 "test-new.c" // 更改下一行的行号和源文件名
int main()              // line 100
{
    printf("%dn", __LINE__); // line 102
    printf("%dn", __LINE__); // line 103
    printf("%sn", __FILE__);
    return 0;
}
输出为:

[root@bogon test-dir]# gcc -o test test.c 
[root@bogon test-dir]# ./test 
102
103
test-new.c


2.4 __func__ 和 __FUNCTION__

作用:代表当前函数的函数名,类型为字符串常量;

#include
int main()
{
    printf("%sn", __func__);
    printf("%sn", __FUNCTION__);
    return 0;
}
输出为:

[root@bogon test-dir]# gcc -o test test.c 
[root@bogon test-dir]# ./test 
main
main


2.5 __DATE__

作用:代表日期,形式为Mmm dd yyyy 的字符串常量;

#include
 
int main()
{
    printf("%sn", __DATE__);
    return 0;
}
输出为:

[root@bogon test-dir]# gcc -o test test.c 
[root@bogon test-dir]# ./test 
Aug  9 2020


2.6 __TIME__

作用:代表时间,hh:mm:ss 形式的字符串型常量;

#include

int main()
{
    printf("%sn", __TIME__);
    return 0;
}
输出为:

[root@bogon test-dir]# gcc -o test test.c 
[root@bogon test-dir]# ./test 
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925843.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号