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.