What is the meaning of "f" in C's printf?
The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read.
%f. a floating point number for floats. %u. int unsigned decimal. %e.
The format specifier %6.2f says to output a floating-point number in a field (num- ber of spaces) of width 6 (room for six characters) and to show exactly two digits after the decimal point.
The f
in printf
stands for formatted, its used for printing with formatted output.
As others have noted, the trailing f
indicates formatted output (or formatted input for functions in the scanf
family).
However, I'll add that the distinction matters because it's important for callers to know that the string is expected to have format-specifier semantics. For example, do not do this:
char* s = get_some_user_input();
printf(s); // WRONG. Instead use: printf("%s", s) or fputs(stdout, s)
If s
happens to contain %
characters, printing it directly with printf
can cause it to access non-existent arguments, leading to undefined behavior (and this is a cause for some security vulnerabilities). Keep this naming convention in mind if you ever define your own printf
-like variadic functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With