r/C_Programming • u/International_Hat178 • Oct 16 '26
Question Why not specifying what to include ?
Hi, sorry if there is English mistake, it's not my first language.
I found online that the #include is replaced by preprocessor by the whole file being included, and I wondered why C isn't made so we can specify only parts of the files to include (like `import { x, y, z } from "file"` in typescript) so the compiled file is lighter ?
20
Upvotes
76
u/pfp-disciple Oct 16 '26
C is much older, and its design is much more straightforward. The behavior you're describing could be crudely done with macros at compile time. See
#ifdef.Modern linkers (and probably not so modern ones) will eliminate unused code, anyway. So, if a file is included that has functions a(), b(), c,(), and d() but only b() and d() are used, the final executable won't have the code for a() and c().
15
u/SirCodedRed Oct 16 '26
It’s also important to note that the final binary file only has the code you used and this is done by the linker.
14
u/SmokeMuch7356 Oct 16 '26
You're not importing code as such; you're just loading symbol and function declarations so that your code will compile.
For example, C requires functions to be declared before they can be used. IOW, if you write
and try to build it, the compiler will complain that
printfhasn't been declared.1 There has to be a declaration ofprintfsomewhere before it's called:stdio.hjust contains the declarations forprintfandscanfand other functions, not their actual implementations. It also contains typedefs for theFILEandfpos_ttypes, definitions for macros likeNULL, etc.int.3
u/sciencekm Oct 16 '26 edited Oct 16 '26
You normally only have declarations in the files that you have in #include. The code still has to come from libraries or other C files. You only link the libraries and C object files that you need.
3
u/torsten_dev Oct 16 '26
The preprocessor is not smart.
The compiler needs to read the entire file anyway.
import {x, y, z} wouldn't make your binary smaller than Dead code elimination.
All the globals and functions without
staticwill still need unique names for the linker. C doesn't mangle names, and it doesn't have namespaces.import {x,y,z} only benefit I could see in C is telling you why you includes the header.
9
u/questron64 Oct 16 '26
When you say import in many languages it imports all the code you request at runtime, which is quite expensive if you want all the code from a large module. However, in C an #include is not import. Generally it's used to introduce header files which are intended to be declarations only, and it's not expensive to have all those declarations for most header files. All the functions those declarations refer to get compiled elsewhere at compile time, not a run time like in many scripting languages.
Some large libraries to help you out, though, and split up their header files into smaller chunks and allow you to include them individually, which does help large projects compile a bit quicker. But import and include are fundamentally different things, don't think of them as equivalent.
2
u/Liam_Mercier Oct 16 '26
Historical reasons, complexity of adding module style imports, etc.
There is a system like this in C++ but that is an entirely different language. There is also a lot more of a benefit to reducing compilation times in a language like C++ where you have a lot of instantiations and abstractions, versus a language like C which generally has better compile times.
1
u/mcsuper5 Oct 16 '26
If you only need one or two simple functions, you can just include the prototype before it is called instead of including the header. I'm saying it can work for some functions. No guarantees it is safe even if it works. Standard headers may just indicate prototypes for some functions, but may define some macros usually used when calling the code
These days, loading several hundred lines that usually don't even need to be compiled isn't a big deal.
Since gcc allows you to tell it the code has already been preprocessed. (see "-fpreprocessed" in the gcc manual) (*.i files are treated as mostly preprocessed c.) You could use another method of preprocessing the code. I don't know how standard that is.
You can also call the preprocessor separately. man cpp.
You could use a custom preprocessor, but you'd probably need custom headers and want to automate the preprocessing. (If you do something like this, INCLUDE an explanation in your source of the tool chain and any/all needed files for the tool chain. Nothing like going back to project 15 years later and not remembering how it worked. Been there and done that!)
1
u/septum-funk Oct 19 '26
you're basically doing that, that's what the header is doing by writing types and function signatures. registering something that will be used and defined elsewhere. if you only need to include specific things ya write a smaller header lol
1
u/ReallyEvilRob Oct 16 '26
Because that's not how they designed the preprocessor to work.
0
u/ComradeGibbon Oct 16 '26
Well now wouldn't that be nice.
There isn't any reason that couldn't be added to the language except the C++ standards committee would have a melt down.
-13
u/kinchkun Oct 16 '26
Because c is a boomer language and doesn't have a module system