Where to put include statements, header or source?

Mohit Deshpande picture Mohit Deshpande · Oct 15, 2010 · Viewed 62k times · Source

Should I put the includes in the header file or the source file? If the header file contains the include statements, then if I include that header file in my source, then will my source file have all of the included files that were in my header? Or should I just include them in my source file only?

Answer

schot picture schot · Oct 15, 2010

Only put includes in a header if the header itself needs them.

Examples:

  • Your function returns type size_t. Then #include <stddef.h> in the header file.
  • Your function uses strlen. Then #include <string.h> in the source file.