目录
一、sprintf( )
格式:sprintf(指针(地址),原本形式,转化来源);
解释:将来源的数据以字符形式输出到指针所指向的地方
注意:( ﹁ ﹁ ) ~→那些%d与%f之间的空格也被转化储存进去了
代码示例如下:
int a = 1;
float b = 2.1;
char c = 'a';
int d = 1, e = 2, f;
f = d + e;
char str[20] = { 0 };
char buf[50] = { 0 };
sprintf(str, "%d %f %c", a, b, c);
sprintf(buf, "Sum of %d and %d is %d", d, e, f);
printf("%s\n", str);
printf("%s\n", buf);
数组str:
数组buf:
由此可见,都是转化为字符一个一个储存进去的。
二、sscanf( )
格式:sscanf(读取来源即指针(地址),目标形式,输入位置);
解释:将来源的数据以一定形式输入到输入位置
代码示例如下:
#include <stdio.h>
struct uni
{
int a;
float b;
char c;
};
int main(void)
{
struct uni str = { 1,2.1,'a'};
struct uni temp = { 0 };
char strarr[20] = { 0 };
sprintf(strarr, "%d %f %c", str.a, str.b, str.c);
sscanf(strarr, "%d %f %c", &(temp.a), &(temp.b), &(temp.c));//
printf("%d %f %c\n", temp.a, temp.b, temp.c);
return 0;
}
三、总结
也是可以与我们熟知的printf(),scanf()对比的(。・ω・。)