转自百度百科
for(int loops=0;loops<10;loops++)
{
char FileName[100];
sprintf(FileName,"%d.txt",loops);
savefile(FileName);
}
1
|
/*例子*/
|
1
2
3
4
5
6
7
8
9
10
11
|
#include<stdio.h>/*某个stdio.h*/
int
main()
/*主函数“整数”类型*/
{
char
buffer[50];
/*“字符”类型的数组,下面共有50个元素。*/
int
n,a=5,b=3;
/*三个变量都为“整数”类型,intn中间要有空格*/
n=
sprintf
(buffer,
"%d plus %d is %d"
,a,b,a+b);
/*赋予数值*/
printf
(
"[%s]is a string %d chars long\n"
,buffer,n);
/*“格式输出函数”*/
return
0;
/*“返回零”
也就是程序正常退出*/
}
|
[5 plus 3 is 8] is a string 9 chars long
|
1
2
3
4
5
6
|
char
*buf[60];
char
*who=
"I"
;
char
*whom=
"CSDN"
;
sprintf
(buf,
"%slove%s."
,who,whom);
printf
(
"%s"
,buf);
//输出结果:"IloveCSDN."
|
1
2
3
|
sprintf
(s,
"%-*d"
,4,
'A'
);
//产生"65"
sprintf
(s,
"%#0*X"
,8,128);
//产生"0X000080","#"产生0X
sprintf
(s,
"%*.*f"
,10,2,3.1415926);
//产生"3.14"
|
1
|
sprintf
(s,
"%u"
,&i);
|
1
|
sprintf
(s,
"%08X"
,&i);
|
1
|
sprintf
(s,
"%p"
,&i);
|
1
|
sprintf
(s,
"%0*x"
,2*
sizeof
(
void
*),&i);
|
1
|
intlen=
sprintf
(s,
"%d"
,i);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain()
{
srand
(
time
(0));
chars[64];
intoffset=0;
for
(inti=0;i<10;i++)
{
offset+=
sprintf
(s+offset,
"%d,"
,
rand
()%100);
}
s[offset-1]=
'\n'
;
//将最后一个逗号换成换行符。
printf
(s);
return0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include<stdio.h>
intmain(
void
)
{
charbuffer[200],s[]=
"computer"
,c=
'l'
;
inti=35,j;
floatfp=1.7320534f;
//Formatandprintvariousdata:
j=
sprintf
(buffer,
"String:%s\n"
,s);
//C4996
j+=
sprintf
(buffer+j,
"Character:%c\n"
,c);
//C4996
j+=
sprintf
(buffer+j,
"Integer:%d\n"
,i);
//C4996
j+=
sprintf
(buffer+j,
"Real:%f\n"
,fp);
//C4996
//Note:sprintfisdeprecated;considerusingsprintf_sinstead
printf
(
"Output:\n%s\ncharactercount=%d\n"
,buffer,j);
}
Copy
Output:
String:computer
Character:l
Integer:35
Real:1.732053
charactercount=79
|