Search code examples
cheaderprototypefunction-definition

Should the header file containing function prototypes be included in file with definitions?


I was trying to find out what would be best practice.

Suppose I have a C-function

void myfunc(double);

and I store this function prototype in myfunc.h

I write the function definitions in myfunc.c


void myfunc(double p){
/*
 * Do work
 */
}

Should I #include "myfunc.h" in myfunc.c?

It isn't necessary but it would seem to me to be better practice.


Solution

  • Yes you should because it makes sure that the function signature is the same in the declaration and the definition. If they don't match the program won't compile. It is also a good idea to include myfunc.h before any other include file in myfunc.c. That way you will know that the header file is self-contained.