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

Constantin picture Constantin · Dec 3, 2008 · Viewed 43.7k times · Source

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

Answer

Chris Young picture Chris Young · Dec 3, 2008

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.