C++ allows extra arguments to printf
because it follows at least a few of the fundamental principles of C, from which printf
was inherited.
The fundamental principle of C which is of interest here is that the compiler should be kept as separate as possible from the standard library. In C it was possible to develop systems without using the standard library at all; you could re-invent whatever parts of the standard library you needed by issuing native calls to your target environment, and you could build any kind of system you could imagine. It would not be portable, but that's a different story.
In early versions of C++ the same held true, though it was a bit more difficult. I do not know whether this is possible in modern C++, but the general philosophy is still there.
Because of this fundamental principle, the compiler does not have, or is not expected to have, any knowledge whatsoever of the printf
function, or of any other standard library function. The language provides a syntax for variadic functions, so that functions like printf
can be written, and that is as far as the language goes.
This is a concept that many folks coming from other languages found hard to grasp: Strictly speaking, the question "how do you print something in C?" does not make sense, and it is only answerable in a roundabout way: technically, you do not print anything in C, because C is a language, and it is not concerned with such mundane concepts like printing. However, the creators of the language were so kind as to provide a standard library, and in order to print something using the C standard library you use the printf
function.
So, since the compiler has no knowledge of the printf
function, it cannot have a saying as to whether the number of arguments that you are passing to it is correct or not. It just does not know, and does not care.
Compilers that do, nonetheless, issue warnings on improper use of printf
are performing a bit of a blasphemy: they are assuming knowledge of the fact that you are most likely using the standard libraries, which is above and beyond their duties, and they do it for the sake of the programmer's convenience.
std::format
andprintf
are very different functions; which do you mean to ask about? For printf it's just because of how C's varargs work. $\endgroup$printf
-style format strings by declaring the function with theformat
attribute. $\endgroup$