1strfromd(3) Library Functions Manual strfromd(3)
2
3
4
6 strfromd, strfromf, strfroml - convert a floating-point value into a
7 string
8
10 Standard C library (libc, -lc)
11
13 #include <stdlib.h>
14
15 int strfromd(char str[restrict .n], size_t n,
16 const char *restrict format, double fp);
17 int strfromf(char str[restrict .n], size_t n,
18 const char *restrict format, float fp);
19 int strfroml(char str[restrict .n], size_t n,
20 const char *restrict format, long double fp);
21
22 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
23
24 strfromd(), strfromf(), strfroml():
25 __STDC_WANT_IEC_60559_BFP_EXT__
26
28 These functions convert a floating-point value, fp, into a string of
29 characters, str, with a configurable format string. At most n charac‐
30 ters are stored into str.
31
32 The terminating null byte ('\0') is written if and only if n is suffi‐
33 ciently large, otherwise the written string is truncated at n charac‐
34 ters.
35
36 The strfromd(), strfromf(), and strfroml() functions are equivalent to
37
38 snprintf(str, n, format, fp);
39
40 except for the format string.
41
42 Format of the format string
43 The format string must start with the character '%'. This is followed
44 by an optional precision which starts with the period character (.),
45 followed by an optional decimal integer. If no integer is specified
46 after the period character, a precision of zero is used. Finally, the
47 format string should have one of the conversion specifiers a, A, e, E,
48 f, F, g, or G.
49
50 The conversion specifier is applied based on the floating-point type
51 indicated by the function suffix. Therefore, unlike snprintf(), the
52 format string does not have a length modifier character. See
53 snprintf(3) for a detailed description of these conversion specifiers.
54
55 The implementation conforms to the C99 standard on conversion of NaN
56 and infinity values:
57
58 If fp is a NaN, +NaN, or -NaN, and f (or a, e, g) is the conver‐
59 sion specifier, the conversion is to "nan", "nan", or "-nan",
60 respectively. If F (or A, E, G) is the conversion specifier,
61 the conversion is to "NAN" or "-NAN".
62
63 Likewise if fp is infinity, it is converted to [-]inf or [-]INF.
64
65 A malformed format string results in undefined behavior.
66
68 The strfromd(), strfromf(), and strfroml() functions return the number
69 of characters that would have been written in str if n had enough
70 space, not counting the terminating null byte. Thus, a return value of
71 n or greater means that the output was truncated.
72
74 For an explanation of the terms used in this section, see attributes(7)
75 and the POSIX Safety Concepts section in GNU C Library manual.
76
77 ┌───────────────────────────────┬─────────────────────┬────────────────┐
78 │Interface │ Attribute │ Value │
79 ├───────────────────────────────┼─────────────────────┼────────────────┤
80 │ │ Thread safety │ MT-Safe locale │
81 │strfromd(), strfromf(), ├─────────────────────┼────────────────┤
82 │strfroml() │ Async-signal safety │ AS-Unsafe heap │
83 │ ├─────────────────────┼────────────────┤
84 │ │ Async-cancel safety │ AC-Unsafe mem │
85 └───────────────────────────────┴─────────────────────┴────────────────┘
86 Note: these attributes are preliminary.
87
89 ISO/IEC TS 18661-1.
90
92 strfromd()
93 strfromf()
94 strfroml()
95 glibc 2.25.
96
98 These functions take account of the LC_NUMERIC category of the current
99 locale.
100
102 To convert the value 12.1 as a float type to a string using decimal no‐
103 tation, resulting in "12.100000":
104
105 #define __STDC_WANT_IEC_60559_BFP_EXT__
106 #include <stdlib.h>
107 int ssize = 10;
108 char s[ssize];
109 strfromf(s, ssize, "%f", 12.1);
110
111 To convert the value 12.3456 as a float type to a string using decimal
112 notation with two digits of precision, resulting in "12.35":
113
114 #define __STDC_WANT_IEC_60559_BFP_EXT__
115 #include <stdlib.h>
116 int ssize = 10;
117 char s[ssize];
118 strfromf(s, ssize, "%.2f", 12.3456);
119
120 To convert the value 12.345e19 as a double type to a string using sci‐
121 entific notation with zero digits of precision, resulting in "1E+20":
122
123 #define __STDC_WANT_IEC_60559_BFP_EXT__
124 #include <stdlib.h>
125 int ssize = 10;
126 char s[ssize];
127 strfromd(s, ssize, "%.E", 12.345e19);
128
130 atof(3), snprintf(3), strtod(3)
131
132
133
134Linux man-pages 6.04 2023-03-30 strfromd(3)