各种预处理命令 笔记 #if #else #elif #define #undef

#if 要求判断条件为常量表达式,不得包含变量。且返回值为整数。

与 #else #elif #endif 使用。

和 if else 的用法类似。

例如:

 1 #include <stdio.h>
 2 
 3 #define M 100
 4 
 5 int main(){
 6 
 7     #if M==100
 8         printf("already\n");
 9     #else
10         #undef M
11         #define M 10
12     #endif // M
13     printf("%d\n", M);
14     return 0;
15 }

运行结果: already 

      100

#ifdef 用来判断某个宏是否定义,如果已定义则执行后面的代码。

用法:

1 #ifdef  宏名
2 
3     程序段 1
4 
5 #else 
6     
7     程序段 2
8 
9 #endif

类似if的用法,若宏被定义,则执行程序段1,否则执行程序段2。

小知识:

//宏定义 #ifdef _WIN32 是由编译器(ml.exe/ml64.exe)内部定义的。

//_WIN32:Defined for applications for Win32 and Win64. Always defined.

//用来判断是否是WINDOWS平台

 1 #include <stdio.h>
 2 
 3 #define M 100
 4 
 5 int main(){
 6 
 7     #ifdef M
 8         printf("%d\n", M);
 9     #else
10         #undef M
11         #define M 10
12     #endif // M
13 
14     return 0;
15 }
康康代码

未完

猜你喜欢

转载自www.cnblogs.com/Arrokoth/p/12238769.html