sscanf可以认为 string + scanf,sprintf 可以认为 string + printf
#include<stdio.h>
int main(){
int n;
char str[100] = "123";
sscanf(str,"%d",&n);
printf("%d\n",n);
return 0;
}
输出:123
#include<stdio.h>
int main(){
int n = 123;
char str[100];
sprintf(str,"%d",n);
printf("%s\n",str);
return 0;
}
输出:123
#include<stdio.h>
int main(){
int n;
double db;
char str[100] = "2048:3.14,hello",str2[100];
sscanf(str,"%d:%lf,%s",&n,&db,str2);
printf("n=%d, db = %.2f, str2 = %s\n",n,db,str2);
return 0;
}
输出:n=2048, db = 3.14, str2 = hello
#include<stdio.h>
int main(){
int n=12;
double db=3.1415;
char str[100], str2[100]="good";
sprintf(str,"%d:%.2f,%s",n,db,str2);
printf("str=%s\n",str);
return 0;
}
输出:str=12:3.14,good