有关TC里 宏定义的问题。
#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)
main()
{
int x=5,x1=3,x2=8;
PR(d,x);
PRINT1(d,x);
PRINT2(d,x1,x2);
getch();
}
这段程序能顺利通过编译,却不能正常运行。出错信息显示为“浮点格式输出错误,程序终止”。显然TC把printf("value=%format\t")的%format认成了%f,而并非如我们所愿,将d把整个format替换。
什么原因?哪位解释一下?
问题点数:0、回复次数:5Top
1 楼hpho()回复于 2003-06-03 23:41:25 得分 0
#include <stdio.h>
#define NL putchar('\n')
#define PR(format,value) printf(#format,(value))
#define PRINT1(f,x1) PR(value=##%f,x1);NL
#define PRINT2(f,x1,x2) PR(%f,x1);PRINT1(f,x2)
main()
{
int x=5,x1=3,x2=8;
clrscr();
PR(%d,x);
PRINT1(d,x);
PRINT2(d,x1,x2);
getch();
Top
2 楼Yimr(铁脚)回复于 2003-06-07 17:54:30 得分 0
这样并没有解决问题。
谁再解释一下? UPTop
3 楼huigll(会)回复于 2003-06-07 18:18:47 得分 0
#include <stdio.h>
#include <conio.h>
#define NL putchar('\n')
#define PR(format,value) { if(format=='d') printf("value=%d\t",(value));\
else if(format=='f')\
printf("value=%f\t",(value));\
else\
printf("Error fotmat");}\
#define PRINT1(f,x1) PR(f,x1);NL
#define PRINT2(f,x1,x2) PR(f,x1);PRINT1(f,x2)
void main()
{
int x=5,x1=3,x2=8;
PR('d',x);
PRINT1('d',x);
PRINT2('f',0.2,2.0);
getch();
}
Top
4 楼stukov2002(卡拉是头猪)回复于 2003-06-07 19:54:13 得分 0
#define PR(format,value) printf("value=%format\t",(value))
改成:
#define PR(format,value) printf("value=%%"#format"\t",(value))
Top
5 楼Yimr(铁脚)回复于 2003-06-10 19:19:20 得分 0
#define PR(format,value) printf("value=%format\t",(value))
改成:
#define PR(format,value) printf(""#value"=%"#format"\t",(value))
这样是正确的Top




