Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is #include <stdio.h> not required to use printf()?

Session transcript:

> type lookma.c int main() {   printf("%s", "no stdio.h"); }  > cl lookma.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86 Copyright (C) Microsoft Corporation.  All rights reserved.  lookma.c Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation.  All rights reserved.  /out:lookma.exe lookma.obj  > lookma no stdio.h 
like image 783
Constantin Avatar asked Dec 03 '08 10:12

Constantin


People also ask

Why the sky is blue and sunsets are reddish?

Discover why the sky is blue and the sunset is red. When sunlight travels through the atmosphere, blue light scatters more than the other colors, leaving a dominant yellow-orange hue to the transmitted light. The scattered light makes the sky blue; the transmitted light ultimately makes the sunset reddish orange.

Why is the sky blue short answer?

The Short Answer: Sunlight reaches Earth's atmosphere and is scattered in all directions by all the gases and particles in the air. Blue light is scattered more than the other colors because it travels as shorter, smaller waves. This is why we see a blue sky most of the time.

Why sky is blue and clouds are white?

But in a cloud, sunlight is scattered by much larger water droplets. These scatter all colours almost equally meaning that the sunlight continues to remain white and so making the clouds appear white against the background of the blue sky.

Why the sky is blue by Sir James Jeans?

After a time a second dust particle again turns it out of its course, and so on, until finally it enters our eyes by a path as zigzag as that of a flash of lightning. Consequently the blue waves of the sunlight enter our eyes from all directions. And that is why the sky looks blue.


2 Answers

You had originally tagged this C++, but it would appear to be a C program. C will automatically provide an implicit declaration for a function if there is no prototype in scope (such as due to the omission of #include <stdio.h>). The implicit declaration would be:

int printf(); 

Meaning that printf is a function that returns an int and can take any number of arguments. This prototype happened to work for your call. You should #include <stdio.h>

Finally, I should add that the current C standard (ISO/IEC 9899:1999 or colloquially "C99") does not allow implicit declarations, and this program would not conform. Implicit declarations were removed. I believe your compiler does not support C99. C++ also requires correct prototypes and does not do implicit declarations.

like image 140
Chris Young Avatar answered Oct 12 '22 13:10

Chris Young


In strict compliance mode (that means "in theory"), you invoke undefined behaviour (which is bad) when you call a function that takes a variable number of arguments without a prototype declaration of the function in scope. That means that the compiler is allowed to do anything it likes with a program that uses printf() without the prototype from #include <stdio.h> or an equivalent declaration. "Anything it likes" includes working correctly as one of the options; that seems to be the option chosen by your example.

In practice, the code will work OK with most practical compilers even without the formal declaration of the printf() function.

As was pointed out by qrdl, the function was found because the C compiler links with the C library.

Note that Chris Young's comment about C99 and 'implicit int' is accurate, but the rule about 'variable arguments functions must have a prototype in scope' applies to both C89 and C99. Most compilers do not work in a strict C99 compatibility mode by default because there is too much code that would not compile like that.

Chris Young commented:

To clarify, my comment was on C99 removing implicit declarations. By saying "implicit int", I think you are referring to the C89 feature of allowing declarations such as foo(void); to mean int foo(void);, something C99 also removed.

Chris is, of course, correct. There were two 'implicit declaration' features removed from the C99 standard. The foreword to the standard lists them as:

  • remove implicit int
  • remove implicit function declaration

I was not thinking (and hence not writing) clearly enough. Nevertheless, both C89 and C99 require a prototype in scope for functions that take a variable number of arguments.

To illustrate:

extern int pqr(); int main(void) {     int i = pqr(1, 3);     return i; } 

Without the first line, this is a correct C89 fragment with an implicit declaration of the function pqr() as a function that returns an integer (with unspecified arguments). If the first line is replaced by extern pqr();, then this is a correct C89 fragment with an explicit declaration of pqr() as a function that returns an integer (with unspecified arguments), but the return type is 'implicit int'. As written, the function is explicitly declared and has an explicit int return type - but it still has unspecified arguments. I believe that is valid C99 - albeit not wholly desirable. Certainly, GCC (3.4.4) accepts it with the options '-std=c99 -pedantic". Ideally, the function declaration should include the full prototype. (And, if pqr() were defined with ellipsis, that prototype would be required in theory!)

like image 42
Jonathan Leffler Avatar answered Oct 12 '22 12:10

Jonathan Leffler