命名空间
变体
操作

strfromf、strfromd、strfroml

来自 cppreference.com
< c‎ | string‎ | byte
在头文件 <stdlib.h> 中定义
int strfromf( char* restrict s, size_t n, const char* restrict format, float fp );
(自 C23 起)
int strfromd( char* restrict s, size_t n, const char* restrict format, double fp );
(自 C23 起)
int strfroml( char* restrict s, size_t n, const char* restrict format, long double fp );
(自 C23 起)

将浮点数转换为字节字符串。

这些函数等效于 snprintf(s, n, format, fp),区别在于格式字符串仅包含字符 %、一个可选的精度(不包含星号 *)以及一个转换说明符 aAeEfFgG,这些说明符适用于由函数后缀(而不是长度修饰符)指示的类型 doublefloatlong double)。对这些函数使用任何其他格式字符串会导致未定义的行为。

内容

[编辑] 参数

s - 指向要写入的字符字符串的指针
n - 最多可以写入 n - 1 个字符,加上空字符结束符
format - 指向指定如何解释数据的空字符结束的字节字符串的指针
fp - 要转换的浮点数

[编辑] 返回值

如果 n 足够大,则将写入的字符数,不包括结束的空字符。因此,如果返回值非负且小于 n,则表示空字符结束的输出已完全写入。

[编辑] 示例

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    char buffer[32];
    int written;
    const char* format[] = {"%a", "%A", "%e", "%E", "%f", "%F", "%g", "%G"};
 
    for (size_t fmt = 0; fmt != sizeof format / sizeof format[0]; ++fmt)
    {
        written = strfromf(buffer, sizeof buffer, format[fmt], 3.1415f);
        printf("strfromf(... %s ...) = %2i, buffer: \"%s\"\n",
               format[fmt], written, buffer);
    }
    puts("");
 
    for (size_t fmt = 0; fmt != sizeof format / sizeof format[0]; ++fmt)
    {
        written = strfromd(buffer, sizeof buffer, format[fmt], 3.1415);
        printf("strfromd(... %s ...) = %2i, buffer: \"%s\"\n",
               format[fmt], written, buffer);
    }
    puts("");
 
    for (size_t fmt = 0; fmt != sizeof format / sizeof format[0]; ++fmt)
    {
        written = strfroml(buffer, sizeof buffer, format[fmt], 3.1415);
        printf("strfroml(... %s ...) = %2i, buffer: \"%s\"\n",
               format[fmt], written, buffer);
    }
}

输出

strfromf(... %a ...) = 13, buffer: "0x1.921cacp+1"
strfromf(... %A ...) = 13, buffer: "0X1.921CACP+1"
strfromf(... %e ...) = 12, buffer: "3.141500e+00"
strfromf(... %E ...) = 12, buffer: "3.141500E+00"
strfromf(... %f ...) =  8, buffer: "3.141500"
strfromf(... %F ...) =  8, buffer: "3.141500"
strfromf(... %g ...) =  6, buffer: "3.1415"
strfromf(... %G ...) =  6, buffer: "3.1415"
 
strfromd(... %a ...) = 20, buffer: "0x1.921cac083126fp+1"
strfromd(... %A ...) = 20, buffer: "0X1.921CAC083126FP+1"
strfromd(... %e ...) = 12, buffer: "3.141500e+00"
strfromd(... %E ...) = 12, buffer: "3.141500E+00"
strfromd(... %f ...) =  8, buffer: "3.141500"
strfromd(... %F ...) =  8, buffer: "3.141500"
strfromd(... %g ...) =  6, buffer: "3.1415"
strfromd(... %G ...) =  6, buffer: "3.1415"
 
strfroml(... %a ...) = 20, buffer: "0xc.90e5604189378p-2"
strfroml(... %A ...) = 20, buffer: "0XC.90E5604189378P-2"
strfroml(... %e ...) = 12, buffer: "3.141500e+00"
strfroml(... %E ...) = 12, buffer: "3.141500E+00"
strfroml(... %f ...) =  8, buffer: "3.141500"
strfroml(... %F ...) =  8, buffer: "3.141500"
strfroml(... %g ...) =  6, buffer: "3.1415"
strfroml(... %G ...) =  6, buffer: "3.1415"

[编辑] 参考

  • C23 标准 (ISO/IEC 9899:2024)
  • 7.24.1.3 strfromd、strfromf 和 strfroml 函数

[编辑] 参见

将格式化的输出打印到 stdout、文件流或缓冲区
(函数) [编辑]
将字节字符串转换为浮点数
(函数) [编辑]