1

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.

TCD
  • 151
  • 1
  • 7

2 Answers2

4

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.

August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
1

Yes. A thousand times yes! If you don't, it will still "work", but, if you ever change the function definition but not the prototype in the header file (or vice versa), the header file will be wrong, and it will cause the compiler to enforce wrong calls everywhere else. And that would completely defeat the whole point of using function prototypes and header files in the first place.

See also Compiler warning for function defined without prototype in scope?

Steve Summit
  • 45,437
  • 7
  • 70
  • 103