将float转换为char * [英] Converting float to char*

查看:585
本文介绍了将float转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C 中将 float 值转换为 char * c $ c> language

解决方案

 
int ret = snprintf(buffer,sizeof buffer,%f,myFloat);

if(ret <0){
return EXIT_FAILURE;

if(ret> sizeof buffer){
/ *结果被截断 - 调整缓冲区的大小并重试。



$ b $ p
$ b $ p $将会存储 myFloat code>在 myCharPointer 中。尽管如此,确保字符串足够容纳它。

snprintf 是比<$更好的选项c $ c> sprintf ,因为它保证它永远不会超过你在参数2中提供的缓冲区大小。


How can I convert a float value to char* in C language?

解决方案

char buffer[64];
int ret = snprintf(buffer, sizeof buffer, "%f", myFloat);

if (ret < 0) {
    return EXIT_FAILURE;
}
if (ret > sizeof buffer) {
    /* Result was truncated - resize the buffer and retry.
}

That will store the string representation of myFloat in myCharPointer. Make sure that the string is large enough to hold it, though.

snprintf is a better option than sprintf as it guarantees it will never write past the size of the buffer you supply in argument 2.

这篇关于将float转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆