sscanf & sprintf

  关于 sscanf 和 sprintf 用法的总结。

sscanf

int sscanf(const char * str,const char * format,...)从字符串中读取格式化的输入。

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
int day, year;
char weekday[20], month[20], dtm[100];

strcpy( dtm, "Saturday March 25 1989" );
sscanf( dtm, "%s %s %d %d", weekday, month, &day, &year );

printf("%s %d, %d = %s\n", month, day, year, weekday );

return(0);
}

Output

1
March 25, 1989 = Saturday

sprintf

int sprintf(char * str,const char * format,...)将格式化的输出发送到str指向的字符串。

Example

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <math.h>

int main () {
char str[80];

sprintf(str, "Value of Pi = %f", M_PI);
puts(str);

return(0);
}

Output

1
Value of Pi = 3.141593