C語言中的預(yù)編譯包含三種:1.宏定義2.文件包含3.條件編譯,條件編譯指的是滿足一定條件下才進行編譯,它有幾種形式:
(1)#ifdef標(biāo)識符
//程序
#else
//程序
#endif
它的意義為如果定義了標(biāo)識符,則執(zhí)行程序段1,否則執(zhí)行程序段2
或者用以下的形式
# ifdef 標(biāo)識符
//程序
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include
# include
int main()
{
#ifdef DEBUG
printf("debug is running\n");
#else
printf("debug is not running\n");
#endif
system("pause");
return 0;
}
(2)
#ifndef 標(biāo)識符
//程序1
#else
//程序2
#endif
它的含義是如果標(biāo)識符沒有被定義,則執(zhí)行程序段1,否則執(zhí)行程序段2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include
# include
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# include
# include
# 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;
}