条件编译

1)
 
1 #ifdef DEBUG
2 程序1
3 #else
4 程序2
5 #endif

 

 例:
 
1 #include
2 //#define DEBUG
3 int main()
4 {
5 #ifdef DEBUG //条件编译(ifndef DEBUG相反)
6   printf("this is debug\n");
7 #endif
8 }

 

 在有#define DEBUG时会执行打印语句。没有时不会打印。如果没有还想打印可以在编译时加入(gcc -D DEBUG debug.c),也会打印。
 
2)
 
1 #ifndef DEBUG
2 程序1
3 #else
4 程序2
5 #endif
6  
7 与1)相反

 

 3)
 
1 #if ON
2 程序1(真时执行,非0)
3 #else
4 程序2(假时执行,05 #endif

 

 例:
 
 1  1 #include
 2  2 #define ON 0  // 注意这里的0(或1)
 3  3 int main()
 4  4 {
 5  5 #if ON
 6  6   printf("ONONONONON\n");    // 不打印
 7  7 #else
 8  8   printf("OFOFOFOFOFOF\n");  //打印
 9  9 #endif
10 10 }

 

0的时候不会打印,相当于注释,1的时候会打印。

 
 1 #include
 2  
 3 int main()
 4 {
 5 #if 0
 6   printf("ONONONONON\n");
 7   printf("OFOFOFOFOFOF\n");
 8   printf("ONONONONON\n");
 9   printf("OFOFOFOFOFOF\n");
10 #endif
11 }

 

 
这样就不会打印了
 
 
 
 
 
posted @ 2016-12-13 20:25  亮哥vvv  阅读(241)  评论(0编辑  收藏  举报