Header Files in C

I read about C for a while and decided to give a little add to the program, nothing out of the ordinary. My understanding of C headers is that they are “interfaces” (such as java and other languages), but where you can also define a variable that either set values ​​or not.

So I wrote this:

#include <stdio.h> #include <stdlib.h> #include "sample.h" int main(int argc, char** argv) { printf("hello World\n"); add(12, 18); return (EXIT_SUCCESS); } int add(int a, int b){ int value = a+b; printf("value = %d\n", value); return 0; } 

It has a header file that looks like this:

 #ifndef SAMPLE_H_GUARD #define SAMPLE_H_GUARD int add(int a, int b); #endif 

I thought of the header files, and this is where I lost their definition, intended to define the use of add, so all I would have to do was call add. From my understanding, I define add rules and then implement add .... functionality.

In addition, many of the materials I read show a single header file for several C files. Where many projects today have one header for one c, that is, Sample.h belongs to Sample.c and nothing else.

Can someone shed some light on this?

Can I do it like this:

main.c

 #include <stdio.h> #include <stdlib.h> #include "sample.h" int main(int argc, char** argv) { printf("hello World\n"); add(12, 18); return (EXIT_SUCCESS); } 

add.c

 #include <stdio.h> #include <stdlib.h> #include "sample.h" int add(int a, int b){ int value = a+b; printf("value = %d\n", value); return 0; } 

sample.h

 #ifndef SAMPLE_H_GUARD #define SAMPLE_H_GUARD int add(int a, int b); #endif 

I believe in the book I read: C> programming language they had an example of a calculator, broken so, my question is how does C know where add is added? He knows the rules for it based on the header file, I think, but not where the actual implementation is ...

Where they break into files like these, they don’t have something like #include "add.c" , all they do is include a header file in files that implement or use this functionality.

Note: obviously, an example of a calculator, and my example will be different, but fundamentally the same - for those who have a book. I just lost the ability to use header files efficiently and effectively.

+6
source share
2 answers

The header file in C will declare the add function for those modules that need it, but do not define the function. The function still needs to be defined in its own module (for example, add.c in your case).

So, to make the foo function available to several modules, you usually:

  • Select the header file (perhaps it belongs if there are other related defines, etc.) to declare foo . For example, perhaps foo.h have void foo(...);

  • In some module, perhaps foo.c , you should define the complete function foo .

  • In any module that wants to call foo , you would #include "foo.h" (or any other header that you used) and call the function.

  • When you compile / link code, you must make sure that all modules, including foo.o or any module having foo defined in it, are present.

The declaration specified in the header file provides (of course) the name of the function, the type of the returned function, and also lists all the parameters and their types. This is all the compiler needs to know to figure out how to call a function from the calling module. During the connection, all addresses are resolved so that the modules know exactly where the function is located in its own specific module.

+4
source

My understanding of C headers is that they are “interfaces” (such as java and other languages), but where you can also define a variable that either set values ​​or not.

This is not true. You cannot “define” variables - well, you can, but this will result in several definition errors when compiling the code if you include the header more than once.

Can I do it like this:

As for your code - both options are correct. C uses headers to read declarations, and therefore headers are also optional. You can split your code into as many as you want .h and .c files. The compiler will create an object file for each .c file. All .h files included in the c file are mainly embedded in this C file "before compilation", that is, at the pre-processing stage. The linker then enters an image that combines objects to create an executable file.

Please feel free if something is unclear in my answer.

+2
source

Source: https://habr.com/ru/post/953503/


All Articles