Content-type: text/html; charset=UTF-8
#include <stdlib.h>
int strfromd(char str[restrict .n], size_t n,
const char *restrict format, double fp);
int strfromf(char str[restrict .n], size_t n,
const char *restrict format, float fp);
int strfroml(char str[restrict .n], size_t n,
const char *restrict format, long double fp);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strfromd(), strfromf(), strfroml():
__STDC_WANT_IEC_60559_BFP_EXT__
The terminating null byte ('\0') is written if and only if n is sufficiently large, otherwise the written string is truncated at n characters.
The strfromd(), strfromf(), and strfroml() functions are equivalent to
snprintf(str, n, format, fp);
The conversion specifier is applied based on the floating-point type indicated by the function suffix. Therefore, unlike snprintf(), the format string does not have a length modifier character. See snprintf(3) for a detailed description of these conversion specifiers.
The implementation conforms to the C99 standard on conversion of NaN and infinity values:
Likewise if fp is infinity, it is converted to [-]inf or [-]INF.
A malformed format string results in undefined behavior.
| Interface | Attribute | Value |
| strfromd(), strfromf(), strfroml() | Thread safety | MT-Safe locale |
| Async-signal safety | AS-Unsafe heap | |
| Async-cancel safety | AC-Unsafe mem |
Note: these attributes are preliminary.
#define __STDC_WANT_IEC_60559_BFP_EXT__ #include <stdlib.h> int ssize = 10; char s[ssize]; strfromf(s, ssize, "%f", 12.1);
To convert the value 12.3456 as a float type to a string using decimal notation with two digits of precision, resulting in "12.35":
#define __STDC_WANT_IEC_60559_BFP_EXT__ #include <stdlib.h> int ssize = 10; char s[ssize]; strfromf(s, ssize, "%.2f", 12.3456);
To convert the value 12.345e19 as a double type to a string using scientific notation with zero digits of precision, resulting in "1E+20":
#define __STDC_WANT_IEC_60559_BFP_EXT__ #include <stdlib.h> int ssize = 10; char s[ssize]; strfromd(s, ssize, "%.E", 12.345e19);