C/C++几个预定义的宏
2016-11-07 14:24
120 查看
一边情况下,C/C++编译器会内置几个宏,这些宏定义不仅可以帮助我们完成跨平台的源码编写,灵活使用也可以巧妙地帮我们输出非常有用的调试信息。
ANSI
C标准中有几个标准预定义宏(也是常用的):
__LINE__:在源代码中插入当前源代码行号;
__FILE__:在源文件中插入当前源文件名;
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;
__STDC__:当要求程序严格遵循ANSI
C标准时该标识被赋值为1;
__cplusplus:当编写C++程序时该标识符被定义。
编译器在进行源码编译的时候,会自动将这些宏替换为相应内容。
下面的代码,不仅展示了各个预定义宏的使用,还介绍了各个数据类型的长度(一般需要在开发芯片前测试一遍)。
#include <stdio.h>
int main(void) {
int answer = 0;
short x = 1;
long y = 2;
float u = 3.0;
double v = 4.4;
long double w = 5.55;
char c = 'p';
// __DATE__, __TIME__, __FILE__, __LINE__ 为预定义宏
printf("Date : %s\n", __DATE__);
printf("Time : %s\n", __TIME__);
printf("File : %s\n", __FILE__);
printf("Line : %d\n", __LINE__);
printf("Enter 1 or 0 : ");
scanf("%d", &answer);
// 这是一个条件表达式
printf("%s\n", answer?"You sayd YES":"You said NO");
// 各种数据类型的长度
printf("The size of int %d\n", sizeof(answer));
printf("The size of short %d\n", sizeof(x));
printf("The size of long %d\n", sizeof(y));
printf("The size of float %d\n", sizeof(u));
printf("The size of double %d\n", sizeof(v));
printf("The size of long double %d\n", sizeof(w));
printf("The size of char %d\n", sizeof(c));
}
输出结果:
Date : 7 11 2016
Time : 13:00:22
File : main.c
Line : 21
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
ANSI
C标准中有几个标准预定义宏(也是常用的):
__LINE__:在源代码中插入当前源代码行号;
__FILE__:在源文件中插入当前源文件名;
__DATE__:在源文件中插入当前的编译日期
__TIME__:在源文件中插入当前编译时间;
__STDC__:当要求程序严格遵循ANSI
C标准时该标识被赋值为1;
__cplusplus:当编写C++程序时该标识符被定义。
编译器在进行源码编译的时候,会自动将这些宏替换为相应内容。
下面的代码,不仅展示了各个预定义宏的使用,还介绍了各个数据类型的长度(一般需要在开发芯片前测试一遍)。
#include <stdio.h>
int main(void) {
int answer = 0;
short x = 1;
long y = 2;
float u = 3.0;
double v = 4.4;
long double w = 5.55;
char c = 'p';
// __DATE__, __TIME__, __FILE__, __LINE__ 为预定义宏
printf("Date : %s\n", __DATE__);
printf("Time : %s\n", __TIME__);
printf("File : %s\n", __FILE__);
printf("Line : %d\n", __LINE__);
printf("Enter 1 or 0 : ");
scanf("%d", &answer);
// 这是一个条件表达式
printf("%s\n", answer?"You sayd YES":"You said NO");
// 各种数据类型的长度
printf("The size of int %d\n", sizeof(answer));
printf("The size of short %d\n", sizeof(x));
printf("The size of long %d\n", sizeof(y));
printf("The size of float %d\n", sizeof(u));
printf("The size of double %d\n", sizeof(v));
printf("The size of long double %d\n", sizeof(w));
printf("The size of char %d\n", sizeof(c));
}
输出结果:
Date : 7 11 2016
Time : 13:00:22
File : main.c
Line : 21
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
相关文章推荐
- 复习几个C++概念:声明与定义、传值与拷贝构造、初始化和赋值
- 【C++中几个预定义变量的介绍】
- 几个C/C++语言log宏定义
- C++中的声明,定义和赋值几个概念的理解
- C/c++几个预定义宏
- C/c++几个预定义的宏:__DATE__,__TIME__,__FILE__,__LINE__
- C++中几个预定义变量的介绍
- C++定义中short int long类型的变量分别占用了几个字节的存储空间
- C/c++几个预定义的宏:__DATE__,__TIME__,__FILE__,__LINE__
- [c++]在类中定义常量的几个做法
- C/c++几个预定义的宏:__DATE__,__TIME__,__FILE__,__LINE__
- C/C++中export的定义
- 关于C#和C++的重载(Overload)、隐藏(Hide)、覆盖(Override)几个区别比较和得出的疑问
- C++中函数中参数和返回值都是用户定义类型(这里主要是类的情况时的分析)
- C++ 变量和函数的声明和定义
- 查找错误的几个调试方法&一些C/C++的文章&一些零碎知识
- C++构造和析构定义的经验总结
- 几个有趣的C/C++开源库
- 几个在信息化管理平台下的词的定义
- C++中的预定义宏