Reputation: 147
As a beginner in C I was experimenting something, so I excluded the standard library <stdio.h>
and still there is no error. Can someone explain it?
Here is the sample code:
main()
{
printf("hello, world!\n");
}
This program is working same with or without library. Why?
Upvotes: 0
Views: 81
Reputation: 134346
TL;DR -- You're excluding the header file, not the standard library.
If you exclude the header file where the function is having a forward declaration, you'll receive a warning for sure mentioning the "implicit declaration" of the function.
In that case, (invalid as per the latest standards), the function will be assumed to return int
and no check on the number of parameters passed will be there.
However, by default, the generated object file from your source is linked with the default C library libc
which has the function definition present. In this case, the function return type matches the implicit case, so linker happily links the object files together.
So, it successfully finishes the linking and works same.
That said, main()
should be int main(void)
, at least to conform to the standards.
Upvotes: 2
Reputation: 768
While it is recommended to include the standard header files when appropriate, it is not necessarily required. The default return value for a function is int (which you ignore in your code and is perfectly allowable). The notion of function prototypes was added to the language well after there was a substantial body of existing programs and so in order to not break them, function prototypes are optional and the default is to have no prototype which means there is no compiler validation that the types of arguments are correct.
Your program is able to link properly against the standard library and execute correctly.
"Hello World!" is a very simple program, so it would be a mistake to rely on this behavior for anything substantial.
Upvotes: 1