Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of f in printf? [closed]

Tags:

c

What is the meaning of "f" in C's printf?

like image 360
Sharan Avatar asked May 25 '12 21:05

Sharan


People also ask

What does F stand for in 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.

What does %F stand for in C?

%f. a floating point number for floats. %u. int unsigned decimal. %e.

What is the meaning of 6.2 F in C?

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.


2 Answers

The f in printf stands for formatted, its used for printing with formatted output.

like image 182
K-ballo Avatar answered Nov 13 '22 04:11

K-ballo


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.

like image 37
jamesdlin Avatar answered Nov 13 '22 04:11

jamesdlin