Search code examples
cheader-files

When to use #include "file.c" and when to use #include "file.h" in c


In what situations we need to use .h file and when to use the .c file in C.
Are they two alternatives for a same purpose?.
Please explain.


Solution

    1. Never include .c files
    2. If you include something, it should be a .h file.
    3. Code goes into code files, .c.
    4. Headers, .h files, contain only what can be compiled more than once, i.e. declarations, macro definitions, more includes.
    5. If necessary, headers use reinclusion guards.

    Whenever you feel tempted to include a .c file, you instead want to add it to your project so that it gets processed during building, i.e. gets compiled by its own and in a further build step gets linked into the created binary.