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__不知道啥用。