C - Why printf, not just print?

I have very stupid doubts. When I taught my C program for a friend, he asked me: "Why do we use printf , cannot be printg or just print ." I could not answer him. So, is there really a reason to choose printf in C programming?

+6
source share
4 answers

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

+14
source

As others have noted, trailing f indicates formatted output (or formatted input for functions in the scanf family).

However, I will add that the difference matters because it is important that the caller knows that the string must have the 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 contains % characters, printing it directly with printf may cause it to access non-existent arguments, which will lead to undefined behavior (and this is the reason for some security vulnerabilities). Follow this naming convention if you ever define your own printf variational functions.

+1
source

printf allows formatting, but print does not. Also, print does not exist in C. I don’t even know what printg is.

0
source

If I'm not mistaken, printf means "Format formatted data in stdout".

0
source

Source: https://habr.com/ru/post/916707/


All Articles