有時候我們在編程的時候,希望有些代碼在我們需要時編譯,不需要時不編譯,也就是讓它快速注釋,這時候即可以考慮#ifdef和#endif,它們會使我們的編譯器進行選擇性編譯。使用方法如下:
-
#include<iostream>
-
#include<cstdio>
-
-
#define DEBUG //至於這個DEBUG的名字,你們可以隨心定義
-
-
using namespace std;
-
int main(){
-
#ifdef DEBUG //如果你前面改掉了DEBUG的名字,呢么這里記得要改
-
cout<<"Hello World"<<endl;
-
#endif
-
return 0;
-
}
如果你們的電腦沒問題的話,呢么輸出一定是下面這個:
這時我們在#define DEBUG前面打上注釋符:
-
#include<iostream>
-
#include<cstdio>
-
-
//#define DEBUG
-
-
using namespace std;
-
int main(){
-
#ifdef DEBUG
-
cout<<"Hello World"<<endl;
-
#endif
-
return 0;
-
}
運行結果如下:
如果您這時還不懂的話,您只需要記住#ifdef 和 #endif是選擇性編譯組,這時您再返回看以上程序。
