Ignoring the stdio.h file in a C file

I am facing a problem in the below given code.

int main()
{
printf("\nHello Geeks\n\n") ;
return 0 ;
}

In the above mentioned code i left including "#include ". And if i compile and execute this piece of code, the output is printed as expected. But "#include " being the most important thing in a C program, i have ignored it and still the compilation is done without any errors but with warning.

Why is this happening?

Maybe one of the paths included for the compiler contains a stdio.h file.

In C++, this is explicitly an error. No undefined functions allowed, period.

C on the other hand, will assume undefined functions take integers and return integers. int printf(int); This works okay-ish if your function takes nothing but 32-bit sized types -- a reasonable guess on a 32-bit system, potentially disastrous anywhere else. If you tried to declare a FILE * or anything else which really needs stdio.h, it would fail to compile.

On 64-bit, your program will segfault due to the 64-bit pointer being mangled through a 32-bit type. The compiler will warn you, too, though the error is less than obvious -- "truncation from assigned type" or some such, rather than the kind of message you'd expect, "you're putting the wrong type into an undefined function and it will blow up in your face".

In short, don't do that. I've seen lots of code blow up when ported to 64-bit because people didn't bother including something they needed.

If you're using gcc, the generated code is probably not even calling printf. Given a format string without conversion specifiers (e.g. %s) which ends with a newline, a builtin optimization has likely replaced printf with puts.

Everything that Corona explained still applies to puts' pointer argument.

Regards,
Alister

Also, if you're using GCC, printf() is a built-in function - though you should get a warning about "incompatible declaration of built-in function printf()" or similar, since your use of the function gives the implicit declaration mentioned in the previous posts.

My advice: Always use -Werror with GCC.

IIRC, and to be pedantic, the arguments to undeclared functions in C are subject to argument promotion, where all arguments are promoted to the same size. What that distinct size is defined to be is probably one of those "implementation specific" details that break things when code violates the C standard. Like here.

Interestingly (at least to me....), if you don't declare a function in C, the source code for that function has to be implemented using the old K&R style so the arguments are "unpromoted" in the actual function call. With K&R C code, you could pass as many arguments as you wanted to any function call, and in the function itself you could actually use as many or as few as you wanted. That's why the prototype for open() today is:

int open( const char *, int, ... )

With K&R C, the open() implementation could refer to the mode argument if needed, and ignore it if not. And woe betide anyone who called open() without the mode argument when the other args meant it was required.

As you imply, if code calling ANSI C functions works without prototypes, it's due to luck.

So yes, don't do that.

I agree with John Graham's recommendation to always treat warnings as errors. If the programmers who wrote the tool that's converting your source code to an executable binary think there are problems with your code even though it's syntactically correct, they're almost certainly correct. Because they know what their tool is doing with your source code, and you really don't. And if the guys who know what's going on think what you're doing is sketchy, it's probably a really good idea to listen to them. Especially when you consider that they didn't have to put that warning in - they put in extra effort to give you that warning.

The whole thing? That'd be an awful big built-in. It can probably turn it into a built-in for certain basic circumstances but there's a reason it's in libc...

According to the GCC documentation. It could be wrong/omitting some thing though.