69,368
社区成员
发帖
与我相关
我的任务
分享
//分析宏定义的输出格式
#include <Stdio.h>
#define NL putchar('\n')
#define PR(format,value) printf("value=%format\t",(value))
#define PRINT1(f,x1) PR(f,x1);NL
#define PRINT2(f,x1,x2) PR(f,x1);PRINT1(f,x2)
void main()
{
float x=5.0,x1=3.0,x2=8.0;
//char d='f';
PR(d,x);/*我已经将上面那行注释了,d就没有定义过了,但为什么还能正常运行,我又把d换成其它字母,依然可以运行*/
PRINT1(d,x);
PRINT2(d,x1,x2);
}
//在宏替换时对字符串中的字符不与替换,保留原状
//分析宏定义的输出格式
#include <Stdio.h>
#define NL putchar('\n')
#define PR(format,value) printf("value=%"#format"\t",(value))//使用#连接!!!!!!!!!!!
#define PRINT1(f,x1) PR(f,x1);NL
#define PRINT2(f,x1,x2) PR(f,x1);PRINT1(f,x2)
int main()
{
float x=5.0,x1=3.0,x2=8.0;
char d='f';
PR(d,x);/*我已经将上面那行注释了,d就没有定义过了,但为什么还能正常运行,我又把d换成其它字母,依然可以运行*/
PRINT1(d,x);
PRINT2(d,x1,x2);
}
//在宏替换时对字符串中的字符不与替换,保留原状
$ cat main.c
#define NL putchar('\n')
#define PR(format,value) printf("value=%format\t",(value))
#define PRINT1(f,x1) PR(f,x1);NL
#define PRINT2(f,x1,x2) PR(f,x1);PRINT1(f,x2)
int main()
{
float x=5.0,x1=3.0,x2=8.0;
PR(d,x);
PRINT1(d,x);
PRINT2(d,x1,x2);
return 0;
}
$ gcc -E main.c
# 1 "main.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "main.c"
int main()
{
float x=5.0,x1=3.0,x2=8.0;
printf("value=%format\t",(x));
printf("value=%format\t",(x));putchar('\n');
printf("value=%format\t",(x1));printf("value=%format\t",(x2));putchar('\n');
return 0;
}