Why can we use printf without including stdio. h

Source: Internet
Author: User
I read the C programming language again to mourn Dennis Ritchie who passed away recently.

I noticed below statements in Section"4.2 functions returning non-integers", Which explains the question, why can we use printf without including stdio. h.

The function atof must be declared and defined consistently. if atof itself and the call to it in main have inconsistent types in the same source file, the error will be detected by the compiler. but if (as is more likely) Atof Were compiled separately, the mismatch wocould not be detected, Atof Wocould return a double that main wocould treat as an int, and meaningless answers wocould result.
In the light of what we have said about how declarations must match definitions, this might seem surprising. The reason a mismatch can happen is that if there is no function prototype, a function is implicitly declared by its first appearance in an expression , Such
Sum + = atof (line)
If a name that has not been previusly declared occurs in an expression and is followed by a left parentheses, it is declared by context to be a function name, the function is assumed to return an int, and nothing is assumed about its arguments. furthermore, if a function declaration does not include arguments, as in
Double atof ();
That too is taken to mean that nothing is to be assumed about the arguments Atof ; All parameter checking is turned off. this special meaning of the empty argument list is intended to permit older C Programs to compile with new compilers. but it's a bad idea to use it with new C Programs. if the function takes arguments, declare them; If it takes no arguments, use
Void .

So, let's take a look at the hello World sample below:
1 // # include
2
3 int main ( int argc, char * argv [])
4 {
5 printf ( " Hello world! \ n " );
6 return 0 ;
7 }< br>

When the code is compiled, the Statement on line 5 implicitly declares the printf function because it's not declared explicitly. It's the same as if we declared printf:
Int printf ();
And there is no assumption about its argument, so the code can pass the compile phase.

In link phase, unlike C ++, only the function name (function signature doesn' t matter) affects the symbol name. so the printf symbol name can be found from the Standard C library which is linked automatically. as a result, the code above can compile and run successfully.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.