c语言中的“*”号

C语言 码拜 9年前 (2015-05-11) 1578次浏览 0个评论

#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”前面有三个空格。)
是“*”号的作用吗?
是怎么做到的?

30分
一般输出是这样:
int num=123;
printf(“%10d\n” ,  num );  //表示按最小数据宽度为10个字符输出整数num
而如果10这里我们想用一个变量来表示,则会采用:
int len=10 ;
int num=123;
printf(“%*d\n” , len, num );   输出结果与上面相同。
5分
* 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).
5分
   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.
作为一个C程序员,对
 scanf,sscanf,fscanf
 printf,sprintf,fprintf
这类函数的用法,还是要做到“拳不离手,曲不离口”的。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c语言中的“*”号
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!