问个_sprintf的含义

实践是最好的学习 2011-03-29 04:11:30
下面这段代码啥意思?从别的程序看到的
_sprintf(buf, "| %-*.*s", (int)width, (int)width, name);
我看不懂
...全文
293 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-03-29
  • 打赏
  • 举报
回复
比如这个:
//Round(1.234,2) = 1.23
//Round(1.234,0) = 1.0
//Round(123.4,-1) = 120.0
double Round(double dVal, short iPlaces) {
double dRetval;
double dMod = 0.0000001;
if (dVal<0.0) dMod=-0.0000001;
dRetval=dVal;
dRetval+=(5.0/pow(10.0,iPlaces+1.0));
dRetval*=pow(10.0,iPlaces);
dRetval=floor(dRetval+dMod);
dRetval/=pow(10.0,iPlaces);
return(dRetval);
}

double round(double dVal, short iPlaces) //iPlaces>=0
{
unsigned char s[20];
double dRetval;

sprintf(s,"%.*lf",iPlaces,dVal);
sscanf(s,"%lf",&dRetval);
return (dRetval);
}

CSDN不许连续同一个作者三次以上回同一个帖子!
只好借宝地一用了。
参考这个帖子:
http://topic.csdn.net/u/20110328/16/60b54ece-510e-4480-b50a-5c4fe32b59d6.html
闲来无事,整了个采用类似画图算法版本的。拜托坛主和楼主不用给分也别删啊。
//c语言编程.任意输入一个数字N(1<=N<=18),打印一个N*N的矩阵,这个矩阵最外圈为1,每往内一圈,数字加1。
#include <stdio.h>
#include <stdlib.h>
char M[19][19];
int N,n,y,x,i,k;
void main() {
while (1) {
printf("\nInput N(1..18):");
if (1==scanf("%d",&N)) {
if (1<=N && N<=18) break;
}
}
//为测试1~18结果的正确性,忽略输入的N。
for (n=1;n<=18;n++) {
printf("------%d-------\n",n);
for (i=1;i<=n;i++) {
y=i ;for (x=i;x<=n+1-i;x++) M[y][x]=i;
y=n+1-i;for (x=i;x<=n+1-i;x++) M[y][x]=i;
x=i ;for (y=i;y<=n+1-i;y++) M[y][x]=i;
x=n+1-i;for (y=i;y<=n+1-i;y++) M[y][x]=i;
}
for (y=1;y<=n;y++) {
for (x=1;x<=n;x++) {
printf("%d",M[y][x]);
}
printf("\n");
}
}
}
//
//Input N(1..18):1
//------1-------
//1
//------2-------
//11
//11
//------3-------
//111
//121
//111
//------4-------
//1111
//1221
//1221
//1111
//------5-------
//11111
//12221
//12321
//12221
//11111
//------6-------
//111111
//122221
//123321
//123321
//122221
//111111
//------7-------
//1111111
//1222221
//1233321
//1234321
//1233321
//1222221
//1111111
//------8-------
//11111111
//12222221
//12333321
//12344321
//12344321
//12333321
//12222221
//11111111
//------9-------
//111111111
//122222221
//123333321
//123444321
//123454321
//123444321
//123333321
//122222221
//111111111
//------10-------
//1111111111
//1222222221
//1233333321
//1234444321
//1234554321
//1234554321
//1234444321
//1233333321
//1222222221
//1111111111
//------11-------
//11111111111
//12222222221
//12333333321
//12344444321
//12345554321
//12345654321
//12345554321
//12344444321
//12333333321
//12222222221
//11111111111
//------12-------
//111111111111
//122222222221
//123333333321
//123444444321
//123455554321
//123456654321
//123456654321
//123455554321
//123444444321
//123333333321
//122222222221
//111111111111
//------13-------
//1111111111111
//1222222222221
//1233333333321
//1234444444321
//1234555554321
//1234566654321
//1234567654321
//1234566654321
//1234555554321
//1234444444321
//1233333333321
//1222222222221
//1111111111111
//------14-------
//11111111111111
//12222222222221
//12333333333321
//12344444444321
//12345555554321
//12345666654321
//12345677654321
//12345677654321
//12345666654321
//12345555554321
//12344444444321
//12333333333321
//12222222222221
//11111111111111
//------15-------
//111111111111111
//122222222222221
//123333333333321
//123444444444321
//123455555554321
//123456666654321
//123456777654321
//123456787654321
//123456777654321
//123456666654321
//123455555554321
//123444444444321
//123333333333321
//122222222222221
//111111111111111
//------16-------
//1111111111111111
//1222222222222221
//1233333333333321
//1234444444444321
//1234555555554321
//1234566666654321
//1234567777654321
//1234567887654321
//1234567887654321
//1234567777654321
//1234566666654321
//1234555555554321
//1234444444444321
//1233333333333321
//1222222222222221
//1111111111111111
//------17-------
//11111111111111111
//12222222222222221
//12333333333333321
//12344444444444321
//12345555555554321
//12345666666654321
//12345677777654321
//12345678887654321
//12345678987654321
//12345678887654321
//12345677777654321
//12345666666654321
//12345555555554321
//12344444444444321
//12333333333333321
//12222222222222221
//11111111111111111
//------18-------
//111111111111111111
//122222222222222221
//123333333333333321
//123444444444444321
//123455555555554321
//123456666666654321
//123456777777654321
//123456788887654321
//123456789987654321
//123456789987654321
//123456788887654321
//123456777777654321
//123456666666654321
//123455555555554321
//123444444444444321
//123333333333333321
//122222222222222221
//111111111111111111
luciferisnotsatan 2011-03-29
  • 打赏
  • 举报
回复
转换后也就是

假设width为2,那么这个格式也就变成了
%-2.2s
  • 打赏
  • 举报
回复
看到了,谢谢
luciferisnotsatan 2011-03-29
  • 打赏
  • 举报
回复
msdn里

Format Specification Fields: printf and wprintf Functions
...
A format specification, which consists of optional and required fields, has the following form:
%[flags] [width] [.precision] [{h | l | ll | I | I32 | I64}]type

width和precision的解释里有
If the width specification is an asterisk (*), an int argument from the argument list supplies the value. The width argument must precede the value being formatted in the argument list. A nonexistent or small field width does not cause the truncation of a field; if the result of a conversion is wider than the field width, the field expands to contain the conversion result.


If the precision specification is an asterisk (*), an int argument from the argument list supplies the value. The precision argument must precede the value being formatted in the argument list.

- . s 这些应该知道的吧




  • 打赏
  • 举报
回复
谁理解得帮忙解释一下"| %-*.*s"是什么意思,怎么会带3个参数的
  • 打赏
  • 举报
回复
我也知道是格式化字符串,但是那个格式化字符串怎么那么奇怪
  • 打赏
  • 举报
回复
我查过MSDN里面的Format好像没有这样的用法
luciferisnotsatan 2011-03-29
  • 打赏
  • 举报
回复
格式化字符串
查MSDN

bdmh 2011-03-29
  • 打赏
  • 举报
回复
用(int)width, (int)width, name填充字符串,到buf中

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧