C++ #if、#elif、#else和#endif指令 的使用
生活随笔
收集整理的這篇文章主要介紹了
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