C语言中的strof函数

解析C字符串str(假定),将其内容解释为浮点数(根据当前语言环境),并将其值返回为浮点数。如果endptr(end pointer)不是空指针,该函数还将endptr的值设置为指向数字后的第一个字符。

null

语法:

strtof(const char* str, char **endptr)
Parameters:
str : String object with the representation of floating point number
endptr : Reference to an already allocated object of type char*, 
whose value is set by the function to the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.
Return Value : On success, the function returns the
 converted floating-point number as a value of type float.

// C code to convert string having
// floating point as its content
// using strtof function
#include <stdio.h>
#include <stdlib.h> // Header file containing strtof function
int main()
{
// Character array to be parsed
char array[] = "365.25 7.0" ;
// Character end pointer
char * pend;
// f1 variable to store float value
float f1 = strtof(array, &pend);
// f2 variable to store float value
float f2 = strtof(pend, NULL);
// Printing parsed float values of f1 and f2
printf ( "%.2f%.2f" , f1, f2);
// Performing operation on the values returned
printf ( " One year has %.2f weeks " , f1 / f2);
return 0;
}


输出:

365.25
7.0
One year has 52.18 weeks

本文由 莫哈克·阿格拉瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享