c语言还可以这么玩

  很长一段时间以来,我都知道C语言可以将多个程序组合成一个程序,没想到,还可以将一个程序分成几个子片段:

  充分利用编译信息的预定义宏:

  1、__FILE__:指出本行语句所在源文件的文件名称,数据类型是字符串

  2、__LINE__:指出本行语句的行号信息,数据类型是整型

  3、__DATE__:

  4、__TIME__:

  5、__STDC__:

 1 //This is c program code!
 2 /* *=+=+=+=+* *** *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
 3   * 文档信息: *** :~/bookCode/learn6/testcc.c
 4   * 版权声明: *** :(魎魍魅魑)MIT
 5   * 联络信箱: *** :guochaoxxl@163.com
 6   * 创建时间: *** :2020年11月14日的下午06:58
 7   * 文档用途: *** :数据结构与算法分析-c语言描述
 8   * 作者信息: *** :guochaoxxl(http://cnblogs.com/guochaoxxl)
 9   * 修订时间: *** :2020年第45周 11月14日 星期六 下午06:58 (第319天)
10   * 文件描述: *** :自行添加
11  * *+=+=+=+=* *** *+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/
12 #include <stdio.h>                                                                      
13 
14 int main(int argc, char **argv)
15 {
16     printf("The fileName: %s
", __FILE__);
17     printf("The lineNumber: %d
", __LINE__);
18 
19     return 0;
20 }

结果:

1 The fileName: testcc.c
2 The lineNumber: 17

  或者用#line  n指定下一行代码的行数或者用#line  n  "fileName"指定特定文件名中的行数为下一行代码:testc1.c  

 1 #include <stdio.h>
 2 #line 0  
 3 int main(void){  
 4     printf("line 1
");
 5     printf("line 2
");
 6     printf("line 3
");
 7 #include "testc2.c" 

  testc2.c

 1 #line 4 "testc1.c"                                                                      
 2 #define LINUX  
 3 #ifdef WIN32  
 4   printf("win32
");   
 5 #elif defined LINUX  
 6   printf("linux
 %d
 %s
",__LINE__,__FILE__);
 7 #else  
 8 #error no flag define   
 9   //如果LINUX和WIN32没有定义,#error会显示错误信息,然后停止编译  
10 #endif  
11 } 

  编译:

1 gcc testc1.c  -o  testc1

  结果:

1 line 1
2 line 2
3 line 3
4 linux
5  8
6  testc1.c

  代码比较简单,没什么说明。

  

原文地址:https://www.cnblogs.com/guochaoxxl/p/13973838.html