在c语言中我们常用sprintf来进行字符串的拼接,但其实sscanf,sprintf两个函数能够实现字符串与其他类型的转换
字符串转整形
#include <string.h>
#include <stdio.h>
int main(){
char str[]="123 838";
int a,b;
sscanf(str,"%d %d",&a,&b);
return 0;
}
字符串转浮点型
#include <string.h>
#include <stdio.h>
int main(){
char str[]="12.3 8.38";
double a,b;
sscanf(str,"%lf %lf",&a,&b);
return 0;
}
整形转字符串
#include <string.h>
#include <stdio.h>
int main(){
char str[20]={0};
int a=123,b=838;
sprintf(str,"%d %d",a,b);
return 0;
}
浮点型转字符串
#include <string.h>
#include <stdio.h>
int main(){
char str[]={0};
double a=12.3,b=8.38;
sprintf(str,"%lf %lf",a,b);
return 0;
}
转换成其它类型类似