提交 2e6e4ce6 编写于 作者: F feilong

add macro

上级 6ff49511
......@@ -6,5 +6,10 @@
"C语言"
],
"children": [],
"export": []
"export": [
"define_constant.json",
"define_func_1.json",
"define_func_2.json",
"define_func_3.json"
]
}
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "define_constant.md"
}
\ No newline at end of file
# 使用宏定义常量
使用宏定义常量定义字符串,输出"Hello,World!",下面错误的选项是:
## 答案
```c
#include <stdio.h>
#define HELLO "Hello,"
#define WORLD "World!"
#define HELLO_WORLD HELLO##WORLD
int main(int argc, char** args){
printf(HELLO_WORLD);
return 0;
}
```
## 选项
### 直接定义宏字符串常量
```c
#include <stdio.h>
#define HELLO_WORLD "Hello,World!"
int main(int argc, char** args){
printf(HELLO_WORLD);
return 0;
}
```
### 两个字符串常量组合
```c
#include <stdio.h>
#define HELLO "Hello,"
#define WORLD "World!"
int main(int argc, char** args){
printf(HELLO WORLD);
return 0;
}
```
### 宏直接拼接
```c
#include <stdio.h>
#define HELLO "Hello,"
#define WORLD "World!"
#define HELLOWORLD HELLO WORLD
int main(int argc, char** args){
printf(HELLOWORLD);
return 0;
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "define_fun_3.md"
}
\ No newline at end of file
# 使用宏定义函数(3)
当一个宏参数被放进宏体时,这个宏参数会首先被全部展开。定义一个宏,用来打印另外一个宏调用展开后的表达式。例如当调用`printf(MACRO_2_STR( ADD( ADD( 1,000 ), 000 ) ));`时,可以输出展开后的表达式`1+000+000`
下面哪个选项输出`1+000+000`
## 答案
```c
#include <stdio.h>
#define ADD( x, y ) x+y
#define TO_STR(x) #x
#define MACRO_2_STR( x ) TO_STR( x )
int main(int argc, char** args){
printf(MACRO_2_STR( ADD( ADD( 1,000 ), 000 ) ));
return 0;
}
```
## 选项
### 选项1
```c
#include <stdio.h>
#define ADD( x, y ) x+y
#define MACRO_2_STR( x ) #x
int main(int argc, char** args){
printf(MACRO_2_STR( ADD( ADD( 1,000 ), 000 ) ));
return 0;
}
```
### 选项2
```c
#include <stdio.h>
#define ADD( x, y ) x+y
int main(int argc, char** args){
printf("%d",ADD( ADD(1,1000), 000 ) );
return 0;
}
```
### 选项3
```c
#include <stdio.h>
#define CONCAT( x, y ) x##y
int main(int argc, char** args){
printf("%d",CONCAT( CONCAT(1, 1000), 000 ) );
return 0;
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "define_fun_1.md"
}
\ No newline at end of file
# 使用宏定义函数(1)
定义一个宏函数,将符号转成字符串,下面代码正确的选项是?
## 答案
```c
#include <stdio.h>
#define TO_STR(s) #s
int main(int argc, char** args){
printf(TO_STR(1000));
printf(TO_STR(HelloWorld!));
return 0;
}
```
## 选项
### 错误实现
```c
#include <stdio.h>
#define TO_STR(s) "s"
int main(int argc, char** args){
printf(TO_STR(1000));
printf(TO_STR(HelloWorld!));
return 0;
}
```
### 错误调用
```c
#include <stdio.h>
#define TO_STR(s) #s
int main(int argc, char** args){
printf(TO_STR(1000));
printf(TO_STR(Hello,World!));
return 0;
}
```
### 凑巧
```c
#include <stdio.h>
#define TO_STR(s) ##s
int main(int argc, char** args){
printf(TO_STR(1000));
printf(TO_STR(HelloWorld!));
return 0;
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "幻灰龙",
"source": "define_fun_2.md"
}
\ No newline at end of file
# 使用宏定义函数(2)
定义一个拼接符号的宏,例如
* 使用 `CONCAT(1,000)` 拼接获得`1000`
* 使用 `CONCAT(1,00)` 拼接获得`100`
下面代码正确的选项是?
## 答案
```c
#include <stdio.h>
#define CONCAT(x,y) x##y
int main(int argc, char** args){
printf("%d", CONCAT(1,000));
return 0;
}
```
## 选项
### 一个井号
```c
#include <stdio.h>
#define CONCAT(x,y) x#y
int main(int argc, char** args){
printf("%d", CONCAT(1,000));
return 0;
}
```
### 加法
```c
#include <stdio.h>
#define CONCAT(x,y) x+y
int main(int argc, char** args){
printf("%d", CONCAT("1","000"));
return 0;
}
```
### 凑巧
```c
#include <stdio.h>
#define CONCAT(x,y) x*1000+y
int main(int argc, char** args){
printf("%d", CONCAT(1,000));
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.
先完成此消息的编辑!
想要评论请 注册