0

this is my 3rd day into C, i just noticed that my program I/O works without #include <stdio.h>. So i am wondering does same apply for all other directives?

#include <stdio.h>
Edin Osmic
  • 374
  • 5
  • 25
  • 1
    What program i/o? Do you mean printf? – jarmod Feb 20 '22 at 16:18
  • 3
    "It works" is not synonym to "It is ok". The fact is that without the `#include` your program is not ok. You **absolutely need** the prototypes of many functions in the C Standard Library to be in scope before calling (or compiling) them (you invoke **Undefined Behaviour** otherwise). The more correct way to get those prototypes (and the absolute easiest way) is with the proper `#include`. – pmg Feb 20 '22 at 16:18
  • Does this answer your question? [Why have header files and .cpp files?](https://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files) – Irelia Feb 20 '22 at 16:22
  • 2
    Amplifying on what pmg said: in programming in general, but in C in particular, it is really important that your code works *for the right reasons*, not merely that it works, or seems to work, today. It turns out there are lots of things you can do that are a bad idea, or that are downright wrong, but that you might be able to get away with. This is one of those. – Steve Summit Feb 20 '22 at 16:24
  • 1
    You should also think about upgrading your compiler. A modern compiler would have warned you about the errors that result when you forget a necessary `#include` line. If yours didn't warn, it may mean that it is seriously out of date. Or, you may just need to increase its warning level somehow. But, and especially if you're just learning, you definitely want the warnings a modern compiler will give you; there are lots of things that are going to be frustrating and mysterious otherwise. – Steve Summit Feb 20 '22 at 16:24
  • 1
    @SteveSummit The comment you're replying to seems to have been deleted, but it absolutely is allowed (per the letter of the standard; I'm not saying it's a good idea) to write `extern int printf(const char *, ...);` yourself instead of including stdio.h. (This is different from types and macros; declaring `FILE` yourself makes the program not strictly conforming, no matter what definition you give.) – zwol Feb 20 '22 at 16:37
  • @zwoi Thanks. The now-deleted comment concerned calling `printf` with no prototype in scope at all, which is different from calling, say, `atoi` with no prototype in scope at all. – Steve Summit Feb 20 '22 at 16:40

1 Answers1

1

In modern C, you should always declare any function, whether yours or a standard library function, before calling it. Some compilers may provide a default declaration for an undeclared function, but this is an archaic behavior and should be avoided. So, even if your program works without including <stdio.h>, it will not be portable to C implementations that do not provide this old behavior and is not good practice.

Including standard headers is an easy, clear, and safe way to declare standard library functions. The C standard allows a program to declare standard library functions itself, as long as its declarations are suitable matches to the functions. However, this practice should be done only in special situations where including all of a standard header is undesired.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312