C++ #define,#ifndef和#ifdef

有时候我们在编程的时候,希望有些代码在我们需要时编译,不需要时不编译,也就是让它快速注释,这时候即可以考虑#ifdef和#endif,它们会使我们的编译器进行选择性编译。

#include<iostream>  
#include<cstdio>  
using namespace std;
#define TEST 
int main()
{
#ifdef TEST  
	cout << "Hello World" << endl;
#endif  
	return 0;
}

  

如果注释掉#define TEST

#include<iostream>  
#include<cstdio>  
using namespace std;
//#define TEST 
int main()
{
#ifdef TEST  
	cout << "Hello World" << endl;
#endif  
	return 0;
}

  

 注释小#define TEST该#ifdef为#ifndef

#include<iostream>  
#include<cstdio>  
using namespace std;
//#define TEST 
int main()
{
#ifndef TEST  
	cout << "Hello World" << endl;
#endif  
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/hsy1941/p/11697488.html