r/C_Programming • • 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

28 comments sorted by

View all comments

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

int main( void )
{
  printf( "Hello, world\n" );
}

and try to build it, the compiler will complain that printf hasn't been declared.1 There has to be a declaration of printf somewhere before it's called:

int printf( const char * restrict, ... );

int main( void )
{
  printf( "Hello, world\n" );
}

stdio.h just contains the declarations for printf and scanf and other functions, not their actual implementations. It also contains typedefs for the FILE and fpos_t types, definitions for macros like NULL, etc.


  1. As of C99, anyway; for C89 and earlier it would just assume the function returned int.

2

u/Zirias_FreeBSD Oct 16 '26

This, at least as long as header files are used in the way they were intended to be used.

Might want to explicitly add what this means for

[...] so the compiled file is lighter ?

When used correctly, the contents of header files don't add anything to the compiled file, as they are only needed during compilation. Whether (and if so, how much) code is added by using stuff declared in headers depends entirely on linking.