歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言條件編譯

C語言條件編譯

日期:2017/3/1 9:48:07   编辑:Linux編程

C語言中的預編譯包含三種:1.宏定義2.文件包含3.條件編譯,條件編譯指的是滿足一定條件下才進行編譯,它有幾種形式:

(1)#ifdef標識符

//程序

#else

//程序

#endif

它的意義為如果定義了標識符,則執行程序段1,否則執行程序段2

或者用以下的形式

# ifdef 標識符

//程序

#endif

# include <stdio.h>
# include <stdlib.h>

int main()
{
#ifdef DEBUG
printf("debug is running\n");
#else
printf("debug is not running\n");
#endif
system("pause");
return 0;
}

(2)

#ifndef 標識符

//程序1

#else

//程序2

#endif

它的含義是如果標識符沒有被定義,則執行程序段1,否則執行程序段2

# include <stdio.h>
# include <stdlib.h>

int main()
{
#ifndef DEBUG
printf("debug is not running\n");
#else
printf("debug is running\n");
#endif
system("pause");
return 0;
}

(3)#if表達式

//程序1

#else

//程序2

#endif

它的意義為表達式的值為真時,就編譯程序段1,表達式的值為假時,就編譯程序段2

# include <stdio.h>
# include <stdlib.h>
# define HEX 1
int main()
{
int i=10;

#if HEX==1
printf("%x\n",i);
#else
printf("%d\n",i);
#endif
system("pause");
return 0;
}

Copyright © Linux教程網 All Rights Reserved