Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implicit header includes in C

program.c :

int main () {

    hello();
    return 0;
}

tools.c :

void hello (void) {

    printf("hello world\n");
}

Makefile :

program : program.o tools.o

In the set of files of this program I have no tools.h file, even though it compiles fine with no errors, could someone explain the purpose of header files in C programs ?

For now I only have one idea : variables such as structures are required at the compilation layer...

But in my case, if the header file only contains function prototypes, is it longer required to build it ? (the makefile linker syntax is a bit easier to catch).

like image 647
vdegenne Avatar asked Jul 18 '26 01:07

vdegenne


1 Answers

Header files usually would contain declarations of the functions which are defined in source c files.

What purpose does it serve?

  • It gives you additional safety, the compiler checks the parameters passed to a function against the declaration and reports errors if it finds an discrepancy.
  • They allow seperation of interface from the implementation.Basically, this allows you to provide your code(implementation) as an library, which clients need to link against while just including the interface header file in their applications.
like image 100
Alok Save Avatar answered Jul 19 '26 13:07

Alok Save



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!