printf的格式化参数是"%.*s\n",从来没这么用过。
麻烦解释一下呸!
[解决办法]
.*
The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
printf("%.*s", 3, "abcdef")
--> abc
就是用附加的参数决定输出字符串的长度
[解决办法]
- C/C++ code
// %.*s 其中的.*表示显示的精度 对字符串输出(s)类型来说就是宽度// 这个*代表的值由后面的参数列表中的整数型(int)值给出// 例如:printf("%.*s\n", 1, "abc"); // 输出aprintf("%.*s\n", 2, "abc"); // 输出abprintf("%.*s\n", 3, "abc"); // 输出abc >3是一样的效果 因为输出类型type = s,遇到'\0'会结束
[解决办法]
我也不知道,呵呵,不过百度了下,写了个程序
- C/C++ code
#include <stdio.h>#include <string.h>int main(){ char * c = "hello"; printf("%.*s\n", strlen(c), c); return 0;}
[解决办法]
[解决办法]
如果在.的前面再加一个参数有意义吗?
对类型s来说没有意义,但对float double来说就有了
printf("%5.*s\n", 5, 3, "abc");
而且这样写也不对,程序会崩
printf("%5.*s\n", 3, "abc");
这样写
[解决办法]
*表示输出位数,具体的数据来自参数表
printf格式字符串中与宽度控制和精度控制有关的常量都可以换成变量,
方法就是使用一个“*”代替那个常量,然后在后面提供变量给“*”
#include <stdio.h>
#include <conio.h>
int main()
{
char *s = "this is test example";
printf("%.*s", 10, s);//这里的常量10就是给*号的,你也可以给他一个变量
//来控制宽度
}
楼主拿去运行下看效果就懂了
[解决办法]
[code=C/C++][/code]
printf("%.*s\n",width,char);
.*意思是要输出字符按串中*个字符,而*要用户输入width来代替。小数点.前也可以添加*,也要用户输入一个位宽值来代替,表示输出的字符所占位宽。
另外请楼主看看scanf中的*修饰符这个部分。和printf不一样,scanf碰到*表示跳过这次输入。
[解决办法]
int a = 123;
printf("%d\n",a); 输出:123
printf("%.*d\n",6,a); 输出:_ _ _ 123 ( 前面的" _ " 代表空格)
%.*s 控制字符串中输出那些字符
%.*d %.*c 控制输出数字或单个字符的宽度, 当让还有左对齐右对齐的问题。