Printf and scanf work without stdio.h, why?

c, c++

Solution

Calling a function without declaring it will create an implicit declaration based on the parameters you give and an assumed return type of `int`. This lets it get past the compilation stage, since the function could exist somewhere else that isn’t known until link time — C didn’t always have function prototypes, so this is for backwards compatibility. (In C++, it’s an error, and in C99 GCC gives a warning.)

If you look at the man page (on FreeBSD and Darwin, at least) for `printf`, `scanf`, `puts`, etc., it says that it comes from the “Standard C Library (libc, -lc)”. GCC implicitly links with the standard C library. If you link with the `-nostdlib` flag, you'll get the “undefined symbols” error that you're expecting.

(In fact, when I turn off `libc`, my GNU/Linux system complains about the absence of `_start` as well, and my OpenBSD system complains about `_start`, `__guard`, and `__stack_smash_handler`.)

Problem

Possible Duplicate: Why #include <stdio.h> is not required to use printf()? Both `printf` and `scanf` have been declared in stdio.h. But they work even without that, dropping just a warning message? What's the theory behind this?

Original source

Related problems