0

In the header file (funlist.h), I listed all the function prototypes such as:

int sum(int a,int b);

float prod(float c,float d);

After that, when I define what "sum" and "prod" are in one C source file (operator.c), do I have to #include "funlist.h" at the beginning?

I compiled and found both include or not include can get the right result. I'm confused why and looking forward to having a explanation.

Tom
  • 3
  • 2
  • 5
  • Welcome. Have a look at [What's the benefit for a C source file include its own header file](https://stackoverflow.com/q/30817570/3258851). Also, I'd advise to use the same name for both the header file and implementation file. – Marc.2377 Jun 02 '18 at 04:41

1 Answers1

1

It's a good practice.

For a more solid reason than that though: If one of your functions calls another defined in operator.c you need to make sure that either a declaration or a definition for that function occurs before the call to it. The best way to do that is to declare all of you functions before you define any. And since the header file is nothing but those declarations, it makes sense to #include it to ensure everything is there.

tdk001
  • 1,014
  • 1
  • 9
  • 16
  • Thanks. You're right, when I call the functions "sum" or "prod" in the `main.c` code, I truly `#include "funlist.h"` at the beginning. As I said, I declared "sum" or "prod" in `funlist.h` and defined them in `operatior.c`, and I tried not to `#include "funlist.h"` in the `operatior.c` because in reality I have hundreds more `operatior.c` that define the function declared in `funlist.h`. And it worked. But, as you said, it's better include the header file in `operatior.c` such that the code looks more professional, am I right? – Tom Jun 02 '18 at 05:02