Hacker News new | ask | show | jobs
by sedatk 18 days ago
It's ironic that these are the times of safety, but when C was getting popular in the 80's, it was the times of power. I clearly remember C was touted as being "so powerful that it lets you do anything without any checks". You were responsible to wield that power properly unlike more strict languages like Pascal, COBOL, or Fortran at the time, yet way more expressive than Assembly of course. Now, the pendulum seems to have swung to the other end.
1 comments

Things have changed so much since then. In the 80s compilers didn’t optimize as well, and generally couldn’t because of resource constraints on the computer. The limited power of 1970s and 1980s computers is all over the design of C. Header files are meant to reduce the memory needed to compile programs, for example.

The last time I needed C’s power to fit something into memory and compute limitations, it was code running on a microcontroller and better systems languages like Zig and Rust weren’t a thing yet.

> Header files are meant to reduce the memory needed to compile programs, for example.

That is not very accurate. Back then, C was compiled file by file and the outputs from those separate compilations were linked into the final binary (each C file is a translation unit, a term still used today). Header files were reprocessed for each new translation unit that included them, they did not save memory for compilation. They saved some memory on disk storage because shared bits of programs were extracted to a common file, but that savings was also not their purpose.

The purpose was to extract the shared bits and reuse them to prevent errors and reduce duplication. Imagine having to recreate the contents of some or all of stdio.h at the top of all your C files that import it. Now you have hundreds of places to update if that library changed, or hundreds of places to make an error in one of the function specs.

Saving memory was not a benefit, nor an intended benefit of header files. The header still has to be read for each translation unit and processed so there are no memory savings in compilation (back then) from using them. Now, today there can be some with precompiled headers, that come much later than the invention of headers for C, though.

That explains a lot. Why do we still have header files? I suppose the C people wouldn’t let us change it at this point, but it does seem quite pointless now that we have computers with 10,000x more memory.
It would be a very disruptive change. For example, you can’t do away with header files without also rethinking how shared libraries work, too. Which then leads to hairy questions like, “Where do you put a library’s preprocessor macros if not in the .h file?” Any such changes you make would need to be coordinated with operating system vendors, since it would touch bits of how shared libraries work that are OS-specific.

And we already have header-only libraries, which are often close enough.