我需要传递浮点数并在程序中获取它.
我尝试了几种方法,但没有成功.这是我的代码:
#include <stdio.h>
int main (int argc, char *argv[])
{
if(argc>1)
{
printf("String \t[%s]\n", argv[1] ); // 222
float floatNumber = atof( argv[1] );
printf("Float \t[%lf]\n", floatNumber ); //0.000000
printf("Float \t[%f]\n", floatNumber ); //0.000000
double doubleValue = atof( argv[1] );
printf("Double \t[%f]\n", doubleValue ); //0.000000
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
你需要#include <stdlib.h>.
atof()声明的函数原型<stdlib.h>.如果不包含此结果,则atof()假定函数返回int.
你会知道你是否打开了编译器警告.例如,gcc给出以下警告:
warning: implicit declaration of function ‘atof’ [-Wimplicit-function-declaration]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
595 次 |
| 最近记录: |