2

C requires different headers files, like stdio.h, stdlib.h, fcntl.h, etc.,for different functions and structure definitions, why is that? Underneath the hood it all boils down to libc so why not create a single header file with all the definitions and prototypes?

Trey
  • 474
  • 2
  • 9
  • 32
  • 2
    In this case each compilation unit will be too big and the compiler can spend much time compiling such a unit. – Vlad from Moscow Oct 13 '17 at 23:28
  • It doesn't require them. They just make life much easier. – David Hoelzer Oct 13 '17 at 23:29
  • 1
    It's not because something is possible that it would also be sane :) It would be a nightmare to maintain with just one header. – Adam Oct 13 '17 at 23:41
  • In some rare cases header files are actually contradictory. It is rare, but one example is if you use the termios2 struct in linux, see https://stackoverflow.com/questions/37710525/including-termios-h-and-asm-termios-h-in-the-same-project – user1048576 Oct 13 '17 at 23:42
  • 1
    Nobody's mentioned the REAL reason here: because C was designed in the 1970s, when the time it took to read a header file from disk actually meant something. Nowadays you could combine all the header files and they would still be read in milliseconds. Back then, having unneeded header files might make your compile take many seconds longer. – Lee Daniel Crocker Oct 14 '17 at 00:29
  • @LeeDanielCrocker: It is difficult for me to say if this would have been a large factor or not. The disk would already be churning to load the source file itself, and there would be at least a few header files being read. By the mid 70's, hard drive latencies was already down to 25ms. – jxh Oct 16 '17 at 23:14

3 Answers3

2

These files are provided by the C standard library to make accomplishing common tasks easier. As to why the declarations and definitions are kept in separate files, it's for convenience and maintainability reasons. The same reason why, for example, the Linux kernel is not defined in a single C file, even though theoretically it could be.

mnistic
  • 10,866
  • 2
  • 19
  • 33
  • 1
    stdio.h and stdlib.h are required by the C standard, in addition to several other headers. – interjay Oct 13 '17 at 23:48
  • they are actually required by the C standard. – Ahmed Masud Oct 13 '17 at 23:48
  • Actually, stdio.h and stdlib.h are not required in a freestanding implementation. They are required in a hosted implementation. fcntl.h is not standard. – Peter Oct 13 '17 at 23:54
  • Take a look at the conformance chapter in the standard. These headers are not required for conformance in what is called the freestanding implementation, as anyone who's worked on embedded systems knows. Type defining headers are the only ones required. But since that sentence seemed controversial I've removed it as it's not even relevant to the question. – mnistic Oct 14 '17 at 14:21
2

The separate header files are defined the way they are largely due to historical compatibility. The C language was already being used on a variety of platforms before it was standardized.

The rationale behind the separate header files was likely a way to mimic modularity within the C language. Separate header files defined functionality provided by a module. This is not too different from how other languages provide functionality.

C also has a philosophy of minimalism, and so requiring every translation unit to compile the prototype declarations of all available functions regardless of whether they were going to be used would seem excessive.

jxh
  • 69,070
  • 8
  • 110
  • 193
1

Such things are not required by C.

It is possible, if you wish, to manually copy the contents of any header file into every source file, and do without the header file. However, that is error prone - if one of the source files changes, it is often necessary to update ALL the source files, and that makes it easy for typos to creep in. (If you copy the content of standard headers to all your compilation units, your code may also fail if built with a different compiler/library, since the content of the headers varies between implementations).

Including header files is simply a technique which allows the content of the header file to be (effectively) copied and pasted into every source file that includes it - and all source files get the SAME content (unless some other preprocessor macros cause things to happen conditionally). For standard headers (particularly those which provide functionality that can only be implemented in different ways on different systems) that aids portability.

Peter
  • 35,646
  • 4
  • 32
  • 74