编译预处理和宏1

 1--

接下来看几个程序

#include <stdio.h>
#define PAI 3.1415926
#define wo "%.7f"
int main() {
	printf(wo,PAI);
}
3.1415926
--------------------------------
Process exited after 0.04554 seconds with return value 0
请按任意键继续. . .

由上可见#define的作用是替换文本,

但是#define不会管字符串即双引号里面的东西。

#include <stdio.h>
#define PAI 3.1415926
int main() {
	printf("PAI");
}
PAI
--------------------------------
Process exited after 0.04434 seconds with return value 0
请按任意键继续. . .

2--还有为什么不用加分号,因为他不是c语言的东西。

 3--如果一个宏有其他的宏的名字,也是会被替换的

#include <stdio.h>
#define PAI2 2*PAI
#define PAI 3.1415926
int main() {
	printf("%f",PAI2);
}
6.283185
--------------------------------
Process exited after 0.04475 seconds with return value 0
请按任意键继续. . .

4--定义不止一行的宏时,如果当前行结束后还有下一行,那么就在当前行加\

#include <stdio.h>
#define PAI2 2*PAI
#define PAI 3.1415926
#define printf	printf("%f\n",PAI);\
				printf("%f\n",2*PAI);\
				printf("%f\n",PAI2)
			
int main() {
	printf;
}
#include <stdio.h>
#define PAI2 2*PAI
#define PAI 3.1415926
#define printf	\
		printf("%f\n",PAI);\
		printf("%f\n",2*PAI);\
		printf("%f\n",PAI2)
			
int main() {
	printf;
}
#include <stdio.h>
#define PAI2 2*PAI
#define PAI 3.1415926
#define printf	\
		printf("%f\n",PAI);printf("%f\n",2*PAI);\
		printf("%f\n",PAI2)
			
int main() {
	printf;
}

 以上三种定义方式均可且输出结果相同。

3.141593
6.283185
6.283185

--------------------------------
Process exited after 0.04351 seconds with return value 0
请按任意键继续. . .

5--宏的值后面的注释不会被当成宏的一部分

#include <stdio.h>
#define PAI 3.1415926  //圆周率 
#define printf printf("%.7f",PAI)

int main() {
	printf;
}
3.1415926
--------------------------------
Process exited after 0.0444 seconds with return value 0
请按任意键继续. . .

6--宏定义时空格不能乱加

 7--还有别把注释//和宏定义的\弄混了。。。

8--这个点没有弄懂

 9--有一些c语言预先编译好的预处理指令

#include <stdio.h>
int main() {
	printf("%s\n%d\n",__FILE__,__LINE__);
	printf("%s\n%s\n",__DATE__,__TIME__);
}
C:\Users\21019\OneDrive\桌面\未命名1.cpp
3
Mar 17 2022
12:37:30

--------------------------------
Process exited after 0.04712 seconds with return value 0
请按任意键继续. . .

还有一个__STDC__不知道啥用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值