c/c++语言开发共享C语言__LINE__实现原理

在test.c中写如下代码: 1 #include <stdio.h> 2 3 int main() 4 { 5 printf("line:%dn", __LINE__); 6 return 0; 7 } 使用gcc编译 gcc -o test test.c 执行 ./test 结果 line:5 …

在test.c中写如下代码:

  1 #include <stdio.h>

  2 

  3 int main()

  4 {

  5     printf(“line:%dn”, __line__);

  6     return 0;

  7 }

使用gcc编译 gcc -o test test.c

执行 ./test

结果 line:5

__line__ 是通过什么方式知道自己在第5行呢?

 

使用命令 gcc -e test.c -o test.i 进行预处理

查看test.i的最后几行代码如下:

535 # 412 “/usr/include/stdio.h” 2 3 4

536 # 2 “test.c” 2

537 

538 int main()

539 {

540     printf(“line:%dn”, 5);

541     return 0;

542 }

由此可见:在预处理阶段,__line__ 会被替换成自己所在行的行号。

 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/605492.html

(0)
上一篇 2021年5月13日
下一篇 2021年5月13日

精彩推荐