c语言中的“*”号,该如何解决
c语言中的“*”号
#include <stdio.h>
void main()
{
printf("%*d\n",i,i);
i++;
printf("%*d\n",i,i);
i++;
printf("%*d\n",i,i);
}
在VC++6.0里运行,结果是:
1
2
3
(“1”前面没有空格,“2”前面有两个空格,“3”前面有三个空格。)
是“*”号的作用吗?
是怎么做到的?
------解决思路----------------------
一般输出是这样:
int num=123;
printf("%10d\n" , num ); //表示按最小数据宽度为10个字符输出整数num
而如果10这里我们想用一个变量来表示,则会采用:
int len=10 ;
int num=123;
printf("%*d\n" , len, num ); 输出结果与上面相同。
------解决思路----------------------
* An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument).
------解决思路----------------------
A field width or precision, or both, may be indicated by an
asterisk * instead of a digit string. In this case, an int argument
supplies the field width or precision.
#include <stdio.h>
void main()
{
printf("%*d\n",i,i);
i++;
printf("%*d\n",i,i);
i++;
printf("%*d\n",i,i);
}
在VC++6.0里运行,结果是:
1
2
3
(“1”前面没有空格,“2”前面有两个空格,“3”前面有三个空格。)
是“*”号的作用吗?
是怎么做到的?
------解决思路----------------------
一般输出是这样:
int num=123;
printf("%10d\n" , num ); //表示按最小数据宽度为10个字符输出整数num
而如果10这里我们想用一个变量来表示,则会采用:
int len=10 ;
int num=123;
printf("%*d\n" , len, num ); 输出结果与上面相同。
------解决思路----------------------
* An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument).
------解决思路----------------------
A field width or precision, or both, may be indicated by an
asterisk * instead of a digit string. In this case, an int argument
supplies the field width or precision.