Headers

As you might have noticed, we included some libraries throughout the examples such as the stdio.h. In the following, we will show how to define own header files and include them into a C program.

Standard Headers

CAUTION: When using non-standard headers, always keep the header files within your working directory and include the header via "header.h" instead of <header.h>. In fact, you can use "header.h" even if using standard headers, but that's not recommended.

There are some standad headers provided by the standard C libraries. Some of those are listed in the following table:

Header FileUse
assert.herror searching and debugging
ctype.hchar testing and converting
errno.herror codes
float.hLimits/Properties for floating points
limits.hConstants
locale.hcountry-specific properties
math.hmathematical functions
setjmp.hunconditional jumps
signal.hSignals
stdarg.hvariable parameter delivery
stddef.hStandard data types
stdio.hStandard-I/O
stdlib.huseful functions
string.hString operations
time.hDate and Time
complex.hcomplex arithmetic (trigonometry etc.)
Fenv.hcontrol of floating point environment
inttypes.hfor precise integer types
iso646.halternative notation for logical operations (in the ISO-646 format)
stdbool.hboolean data types
stdint.hinteger types with a preset width
tgmath.htype-generic mathematical functions
wchar.hwide chars
wctype.hctype for wide chars

Header File

As there are standard headers to include, it is also possible defining own headers. This is especailly useful for codes that one mighty use more often as it spares declaring a function all over again and it also makes sure that a function can be fixed at one place only if there are errors. In the following, a sample header file will be shown.

mathcollection.h

#ifndef MATHCOLLECTION_H_   /* Include guard */
#define MATHCOLLECTION_H_

// code to include in the header
int mod(int a, int b);  /* function declaration */
int gcd(int a, int b);
int power(int num, int exp);

#endif // MATHCOLLECTION_H_

NOTE: The keyword #ifndef makes sure to only define this header if the token is not included already. If included already, the #else block will be used. This example does not utilize any #else block.

Function Definitions

mathcollection.c

#include "mathcollection.h"     // Not neccessary here

int mod(int a, int b){
    while(a >= b)
        a = a - b;
    return a;
}

int gcd(int a, int b){
    if (b == 0)
        return a;
    a = mod(a, b);
    return gcd(b, a);
}

int power(int num, int exp){
    int res = 1;
    while(exp){
        res *= num;
        exp--;
    }
    return num;
}

Main Program

The following C program will include the previously created header file and use the defined functions. The expected results are shown in the comments next to the respective lines.

main.c

#include "mathcollection.h"

int main(){
    int a = 16;
    int b = 3;

    int mod_res = mod(a, b);    // mod(16, 3) = 1 as 15 % 3 = 0
    int gcd_res = gcd(a, b);    // gcd(16, 3) = 1 as 3 is prime
    int pow_res = power(a, b); // 16*16*16

    return 0;
}

Blog Comments powered by Disqus.