提交 8f3a31dc 编写于 作者: F feilong

添加#undef和条件编译

上级 183e4c96
...@@ -6,5 +6,7 @@ ...@@ -6,5 +6,7 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"undef.json"
]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "undef.md"
}
\ No newline at end of file
# 使用 `#undef`
C语言的宏定义可以通过`#undef`取消定义,下面程序的输出是哪个选项?
```c
#include <stdio.h>
#define next(x) x+1
#undef next
int main(int argc, char** args){
printf("%d", next(10));
return 0;
}
```
## 答案
```c
error: use of undeclared identifier 'next'
```
## 选项
### 直接定义宏字符串常量
```c
10
```
### 两个字符串常量组合
```c
11
```
### 宏直接拼接
```c
12
```
\ No newline at end of file
...@@ -6,5 +6,8 @@ ...@@ -6,5 +6,8 @@
"C语言" "C语言"
], ],
"children": [], "children": [],
"export": [] "export": [
"ifdef01.json",
"ifdef02.json"
]
} }
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "ifdef01.md"
}
\ No newline at end of file
# 使用 `#undef`
C语言的宏定义可以通过`#ifdef`,`#else`,`#endif`包含条件编译,下面输出11的程序是?
## 答案
```c
#include <stdio.h>
#define add_encode(x) x+1
#define sub_encode(x) x-1
#define unit_encode(x) x
#define USE_INC
#ifdef USE_INC
#define encode add_encode
#else
#ifdef USE_SUB
#define encode sub_encode
#else
#define encode unit_encode
#endif
#endif
int main(int argc, char** args){
printf("%d", encode(10));
return 0;
}
```
## 选项
### 直接定义宏字符串常量
```c
#include <stdio.h>
#define add_encode(x) x+1
#define sub_encode(x) x-1
#define unit_encode(x) x
#define USE_SUB
#ifdef USE_INC
#define encode add_encode
#else
#ifdef USE_SUB
#define encode sub_encode
#else
#define encode unit_encode
#endif
#endif
int main(int argc, char** args){
printf("%d", encode(10));
return 0;
}
```
### 两个字符串常量组合
```c
#include <stdio.h>
#define add_encode(x) x+1
#define sub_encode(x) x-1
#define unit_encode(x) x
#define USE_UNIT
#ifdef USE_INC
#define encode add_encode
#else
#ifdef USE_SUB
#define encode sub_encode
#else
#define encode unit_encode
#endif
#endif
int main(int argc, char** args){
printf("%d", encode(10));
return 0;
}
```
### 宏直接拼接
```c
#include <stdio.h>
#define add_encode(x) x+1
#define sub_encode(x) x-1
#define unit_encode(x) x
#ifdef USE_INC
#define encode add_encode
#else
#ifdef USE_SUB
#define encode sub_encode
#else
#define encode unit_encode
#endif
#endif
int main(int argc, char** args){
printf("%d", encode(10));
return 0;
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "ifdef02.md"
}
\ No newline at end of file
# 使用 `#undef`
C语言的宏定义可以通过`#ifdef`,`#else`,`#endif`包含条件编译,下面输出10的程序是?
## 答案
```c
#include <stdio.h>
#define DEBUG
int main(int argc, char** args){
#ifdef DEBUG
printf("%d", encode(10));
#else
printf("%d", encode(11));
#endif
return 0;
}
```
## 选项
### 直接定义宏字符串常量
```c
#include <stdio.h>
int main(int argc, char** args){
#ifdef DEBUG
printf("%d", encode(10));
#else
printf("%d", encode(11));
#endif
return 0;
}
```
### 两个字符串常量组合
```c
#include <stdio.h>
#define DEBUG
#undef DEBUG
int main(int argc, char** args){
#ifdef DEBUG
printf("%d", encode(10));
#else
printf("%d", encode(11));
#endif
return 0;
}
```
### 宏直接拼接
```c
#include <stdio.h>
#define DEBUG
int main(int argc, char** args){
#ifdef DEBUG
printf("%d", encode(11));
#else
printf("%d", encode(10));
#endif
return 0;
}
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册