当前位置:
首页 >
C++ #if、#elif、#else和#endif指令 的使用
发布时间:2023/11/27
48
豆豆
生活随笔
收集整理的这篇文章主要介绍了
C++ #if、#elif、#else和#endif指令 的使用
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
#if、#elif、#else和#endif指令 的作用
#if 指令,与 #elif, #else和 #endif 指令,控件源文件的生成。如果表达式编写 (在 #if) 后有一个非零值,在 #if 指令后的行组在该翻译单元保留
#if注意使用:
必须以结束 #endif 指令与源文件中的每个 #if 指令。任意数量的 #elif 指令可以出现在 #if 和 #endif 指令之间,但是,最多一个 #else 指令允许。#else 指令,如果有,则必须是最后一个指令
#if的使用
1 #if 后面跟一个为0的时候里面就不会编译,可以当做多行注释使用如下
#include <iostream>
#include <string>
using namespace std;
int main()
{#if 0
123sdf
adinf adfm iasdf adflkj adsf
adf afd afd adsf
大大ad
暗室逢灯安分阿斯顿发
#endifreturn 0;
};
2 宏定义的时候使用(c++中 true 可以当做 1,false 当做0)
#include <iostream>
#include <string>
using namespace std;
#define POWER 1
int main()
{
#if POWER == 1cout << "power==1" << endl;
#elif POWER == 2cout << "power==1" << endl;
#elsecout << "power==1" << endl;
#endifreturn 0;
};
打印结果:
3 使用defined的情况
这个是说defined(xx) 的意思是xx 有没有被宏定义,如果定义了为true 否则为false
如下 power 被宏定义了,所以打印结果是power==1 具体的如下
#include <iostream>
#include <string>
using namespace std;
#define POWER 1
int main()
{#if defined(POWER)cout << "power==1" << endl;
#elif defined(HELLO)cout << "power==1" << endl;
#elsecout << "power==1" << endl;
#endifreturn 0;
};
总结
以上是生活随笔为你收集整理的C++ #if、#elif、#else和#endif指令 的使用的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 黑死是谁画的呢?
- 下一篇: C++ 预编译的时候使用defined