cc99c-standard-library

Why does the standard C library feature multiple header files instead of consolidating the contents into a single header?


Why does the standard C library need to feature multiple header files? Would it not be more user friendly to consolidate it into one header file?

I understand that it is unnecessary to include function prototypes / global variables that go unused, however, won't the compiler eventually remove all of these references if they're unused?

Maybe I'm underestimating the size of contents spanning all of the header files. But it seems like I'm always googling to figure out what header I need to #include.

Edit: The top comment references a size of 50MB which appears to be untrue. The C library is relatively concise compared to other language's standard libraries hence the question.


Solution

  • // sarcasm ON

    Build your own #include "monster.h" that includes all you want from the collection.

    People write obfuscated code all the time. But, you won't be popular with your cohort.

    // sarcasm OFF

    The real reason? C was developed when storage was barely beyond punch card technology. A multi-user system might have 1/2Mb of (magnetic) core memory and a cycle time that is now laughable.

    Yet, reams of C code was developed (and the story of the popularity of UNIX well known.)

    Although new standards could change the details (and C++ has), there is an ocean of code that might/would no longer be able to be compiled.

    Look up "Legacy".

    There are (too many) examples of code in the world where the usual collection of, say, stdio.h, stdlib.h, etc. have been "hidden" inside #include "myApp.h". Doing so forces the reader to hunt-down a second file to verify what the &^%&%& is going on.

    Get used to it. If it was a bad practice, it would not have survived for ~50 years.


    It is for a similar reason that "functions" have been "grouped together" into different libraries. By extension, pooling those libraries might make things a tiny bit "less cluttered", but the process of linking may require a supercomputer to finish the job before knock-off time.


    Brace yourself. Cleanly written C++ code often has a .h and a .cpp for every class used in the app. Sometimes "families" (hierarchies) might live together in a single pair of files, although that can lead to boo-boo's when the coder is having a bad day. And coders of those apps start to have nervous breakdowns when designing their first class with multiple inheritance. (Recall the scene, early in the movie "Wall-E"; the little robot's hesitant attempts to add the first spork - combination of spoon and fork - to his collection.)