c语言中预处理命令的作用,C语言预处理命令的使用

预处理命令

定义和使用宏

没有参数的宏

#include

# define MAX 100

# define INFO "Uozoyo best lover"

int main(){

printf("the integer is %d, the string is %s", MAX, INFO);

}

输出如下:

the integer is 100, the string is Uozoyo best lover

带参数的宏

#include

# define MAX 100

# define INFO "Uozoyo best lover"

# define print(num, name) printf("the integer is %d, the string is %s", num, name)

int main(){

print(MAX, INFO);

}

输出和上一节相同。

可选参数

使用省略号...表示可选参数。

# define var_print(...) printf("%s variable arguments is %s %s\n",__func__, __VA_ARGS__)

int main(){

var_print("peng", "xiong");

}

输出为main variable arguments is peng xiong,其中__VA_ARGS__是将剩下的所有的参数都传递进去。

字符串化运算符

#被称为字符串化运算符(stringify operator),因为它会把宏调用时的实参转化为字符串。

# define printDBL(exp) printf(#exp "=%f ", exp) // 字符串化运算符#

int main(){

printDBL(atan(1.0)*4);

}

输出为atan(1.0)*4=3.141593

记号粘贴运算符

token-pasting operator会把左右操作数结合在一起,作为一个记号。

# define TEXT_A "hello world, uozoyo." // 记号黏贴运算符

# define msg(x) printf(TEXT_ ## x)

int main(){

msg(A); // 相当于printf(TEXT_A);

}

输出为hello world, uozoyo.。

在宏内使用宏

宏不可以递归的展开。

宏的作用域和重新定义

undef

泛型宏

// Generic selection ----------BUG--------------not solved

# define peng_l(x) printf("i love u forever longtime double")

# define peng_f(x) printf("i love u forever float")

# define uozoyo(x) _Generic((x),\

long double: peng_l,\

float: peng_f,\

default: peng_f)(x)

这里写出来的代码时有问题的

条件式编译

# if defined(__GNUC__)

#pragma message("gunc")

# else

#pragma message("not gunc")

#endif

编译出来的信息为

C:\Users\Administrator\Desktop>gcc test.c

test.c:5:11: note: #pragma message: gunc

#pragma message("gunc")

^~~~~~~

定义行号

# line 2000 "uozoyo.c" // 改变行号和文件名,这里其实可以和__LINE__和__FILE__对应

int main(){

printf("file: %s ",__FILE__);

printf("line: %d",__LINE__);

}

输出如下:file: uozoyo.c line: 2016即改变了原来的文件名和行数。

生成错误消息

# ifndef __STDC__ // 生成错误信息

# error "this compiler doesn't conform to the ANSI C standard."

#endif

生成错误信息并退出。

#pragma命令

#_pragma运算符

预定义的宏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值