linux下c语言字符串转数字的问题

来源:百度知道 编辑:UC知道 时间:2024/05/03 08:42:39
例如我现在有一个字符串“12.34”,想要转换到数字,不知道应该要用什么函数?该怎么写程序?
写了一下,问题来了:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void main()
{
char c[5] = "12.34";
double d;
sscanf(c,"%lf",&d);
printf("%g",d);
}

1.c:1:20: error: syslib.h: No such file or directory
1.c: In function ‘main’:
1.c:8: warning: incompatible implicit declaration of built-in function ‘printf’
1.c:4: warning: return type of ‘main’ is not ‘int’

不知道是怎么了,printf的头文件也该包括在里面了。

很好写啊~
char c[] = "12.34";
double d;
sscanf(c,"%lf",&d);

sscanf的作用是从给定字符串中按照某格式读数据

---

在我的编译器上你的程序没有问题
你按照我写的试试吧

#include <stdio.h>
int main(){
char str[] = "12.34";
double d;
sscanf(str,"%lf",&d);
printf("%lf",d);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
main()
{
char *s;
float n;
s = 12.34;
n = atof(s);
printf("%f\n",n);
}

atof()函数将字符串转换成浮点数