| Functions | |||||||||||||||||||||||||||||||||||||||||
| Character manipulation | |||||||||||||||||||||||||||||||||||||||||
| Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String examination | |||||||||||||||||||||||||||||||||||||||||
| Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)(C11) | |||||||||||||||||||||||||||||||||||||||||
Defined in header <stdlib.h> | ||
int strfromf(char*restrict s,size_t n,constchar*restrict format,float fp); | (since C23) | |
int strfromd(char*restrict s,size_t n,constchar*restrict format,double fp); | (since C23) | |
int strfroml(char*restrict s,size_t n,constchar*restrict format,longdouble fp); | (since C23) | |
Converts a floating-point value to a byte string.
The functions are equivalent tosnprintf(s, n, format, fp), except that the format string shall only contain the character%, an optional precision that does not contain an asterisk*, and one of the conversion specifiersa,A,e,E,f,F,g, orG, which applies to the typedouble,float, orlongdouble) indicated by the function suffix (rather than by a length modifier). Use of these functions with any other format string results in undefined behavior.
Contents |
| s | - | pointer to a character string to write to |
| n | - | up to n - 1 characters may be written, plus the null terminator |
| format | - | pointer to a null-terminated byte string specifying how to interpret the data |
| fp | - | floating-point value to convert |
The number of characters that would have been written hadn been sufficiently large, not counting the terminating null character. Thus, the null-terminated output has been completely written if and only if the returned value is both nonnegative and less thann.
#include <stdio.h>#include <stdlib.h> int main(){char buffer[32];int written;constchar* 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);}}
Output:
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"
(C99)(C11)(C11)(C11)(C11) | prints formatted output tostdout, a file stream or a buffer (function)[edit] |
(C99)(C99) | converts a byte string to a floating-point value (function)[edit] |