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
View all comments
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.