一边情况下,C/C++编译器会内置几个宏,这些宏定义不仅可以帮助我们完成跨平台的源码编写,灵活使用也可以巧妙地帮我们输出非常有用的调试信息。

ANSI C标准中有几个标准预定义宏(也是常用的):
  • __LINE__:在源代码中插入当前源代码行号;
  • __FILE__:在源文件中插入当前源文件名;
  • __DATE__:在源文件中插入当前的编译日期
  • __TIME__:在源文件中插入当前编译时间;
  • __STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1;
  • __cplusplus:当编写C++程序时该标识符被定义。

编译器在进行源码编译的时候,会自动将这些宏替换为相应内容。

下面的代码,不仅展示了各个预定义宏的使用,还介绍了各个数据类型的长度。
   
   
  1. #include <stdio.h>
  2. int main(void) {
  3. int answer;
  4. short x = 1;
  5. long y = 2;
  6. float u = 3.0;
  7. double v = 4.4;
  8. long double w = 5.54;
  9. char c = 'p';;
  10. // __DATE__, __TIME__, __FILE__, __LINE__ 为预定义宏
  11. printf("Date : %s\n", __DATE__);
  12. printf("Time : %s\n", __TIME__);
  13. printf("File : %s\n", __FILE__);
  14. printf("Line : %d\n", __LINE__);
  15. printf("Enter 1 or 0 : ");
  16. scanf("%d", &answer);
  17. // 这是一个条件表达式
  18. printf("%s\n", answer?"You sayd YES":"You said NO");
  19. // 各种数据类型的长度
  20. printf("The size of int %d\n", sizeof(answer));
  21. printf("The size of short %d\n", sizeof(x));
  22. printf("The size of long %d\n", sizeof(y));
  23. printf("The size of float %d\n", sizeof(u));
  24. printf("The size of double %d\n", sizeof(v));
  25. printf("The size of long double %d\n", sizeof(w));
  26. printf("The size of char %d\n", sizeof(c));
  27. }
输出结果:
Date : Feb 11 1997
Time : 13:51:31
File : white.c
Line : 20
Enter 1 or 0 : 1
You sayd YES
The size of int 4
The size of short 2
The size of long 8
The size of float 4
The size of double 8
The size of long double 8
The size of char 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值