by sedatk · 1w ago · 0 comments
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.
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.
And we already have header-only libraries, which are often close enough.