Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should standard library include be writen ? .c or .h file?

Tags:

c

header-files

I have the simple following code :

mainc.c:

    #include <stdlib.h>
    #include "hello.h"


    int main (int argc, char *argv[])
    {
      hello ();
      return EXIT_SUCCESS;
    }

hello.c:

    #include "hello.h"

    void hello (void)
    {
      printf ("Hello world!");
    }

hello.h:

    #ifndef _HELLO_H_
    #define _HELLO_H_
    #endif

I need to include stdio.h in hello to be able to access the printf() function.

Where should I include it ? In hello.c or hello.h ? Is there a best practice as both solutions seem to be correct ?

like image 808
T. Decker Avatar asked Oct 19 '25 23:10

T. Decker


1 Answers

Header files within your application should only include system headers which are required to declare further interfaces within the header.

For example -- if your header includes functions which take a FILE * as a parameter, it should #include <stdio.h>. If it declares a structure containing a uint32_t, it should #include <stdint.h>. And so on.

System headers which are only used within the implementation should be left to the .c file. Your header should not #include <stdio.h> simply because the implementation calls printf(), for example.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!