Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header Files in C

Tags:

c

header-files

I have been reading about C for a while now and decided lets write a little add program, nothing fancy at all. My understanding of C headers is that they are "interfaces" (such as like java and other languages) but where you can also define variable that either have 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 such:

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

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

Also, A lot of the material I have read shows one header file for multiple C files. where as a lot of projects today have one header per one c, meaning Sample.h belongs to Sample.c and nothing else.

Can some one shed some light on this?

Could I have done this like so:

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 was reading: C Programming Language they had a calculator example split up like this, my question is how does C know where add is defined? It knows the rules for it based on the header file, i think, but not where the actual implementation is ....

There example where they split of the files like such doe not have something like #include "add.c" all they do is include the header file in the files that either implement or use this functionality.

Note: obviously the calculator example and my example are going to be different but fundamentally the same - for those who have the book. I am just lost on how to use header files effectively and efficiently.

like image 832
LogicLooking Avatar asked Sep 09 '13 16:09

LogicLooking


People also ask

How many header files are in C?

There are 19 header files in the Standard C Library. All files have the . h file extension.

What is the use of header file in C example?

These preprocessor directives are used for instructing compiler that these files need to be processed before compilation. In C program should necessarily contain the header file which stands for standard input and output used to take input with the help of scanf() and printf() function respectively.

What is header file and library in C?

1 Header Files. Libraries for use by C programs really consist of two parts: header files that define types and macros and declare variables and functions; and the actual library or archive that contains the definitions of the variables and functions.

Where can I find C header files?

Most standard headers are stored in /usr/include . It looks like stdbool. h is stored somewhere else, and depends on which compiler you are using. For example, g++ stores it in /usr/include/c++/4.7.


1 Answers

A header file in C would declare the function add for those modules that need it, but not define the function. The function is still to be defined in its own module (e.g., in your case, add.c).

So in general, to make a function foo available to several modules, you would normally:

  • Choose a header file (maybe it's own if there are other associated defines, etc) to declare foo. For example, perhaps foo.h would have void foo(...);

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

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

  • When you compile/link the code, you would make sure all modules, including foo.o or whatever module has foo defined in it, were present.

A declaration, given in the header file, provides (of course) the function name, the function return type as well as listing all the parameters and their types. This is all the compiler needs to know to figure out how to call the function from the calling module. At link time, addresses are all resolved so that the modules then know exactly where the function is in its own particular module.

like image 133
lurker Avatar answered Sep 21 '22 12:09

lurker