• C Programming Video Tutorials

Header Files in C



The #include preprocessor directive is used to make the definitions of functions, constants and macros etc. from one file, usually called as a header file, available for use in another C code. A header file has ".h" extension from which you can include the forward declarations of one or more predefined functions, constants, macros etc. The provision of header files in C facilitates a modular design of the program.

System Header Files

The C compiler software is bundled with many pre-compiled header files. These are called system header files. A well-known example is "stdio.h" – a header file included in almost every C program.

Each of the system header files contains a number of utility functions. These functions are often called library functions. For example, printf() and scanf() functions, needed for performing IO operations, are the library functions available in the "stdio.h" header file.

The preprocessor statements that load one or more header files are always in the beginning of the C code. We start our journey to learn C programming with a basic "Hello World" program that starts with including the "stdio.h" file

#include <stdio.h>

int main() {

   /* my first program in C */
   printf("Hello, World! \n");

   return 0;
}

The C preprocessing directive #include basically is a request to the compiler to load the contents of a specific header file, so that they can be used in the program.

A usual practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required.

Syntax to Include Header Files in C

A header file is loaded with the #include directive. Its usage follows either of the following two methods −

#include <filename.h>

The name of the header file put inside angular brackets if it is available in system/default directory.

#include "filename.h"   

The name of the header file put inside double quotation marks for user defined or non-standard header files available in same directory as source file

The #include directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current source file. The output from the preprocessor contains the output already generated, followed by the output resulting from the included file, followed by the output that comes from the text after the #include directive.

Standard Header Files in C

A typical C compiler is bundled with a number of pre-compiled header files. Each header file contains the set of predefined standard library functions. The "#include" preprocessing directive is used to include the header files with ".h" extension in the program.

Here is the table that displays some of the header files in C language −

Header Files Description Functions/macros/variables
stdio.h Input/Output functions scanf(), printf(), fopen(), FILE
stdlib.h General utility functions atoi(), atof(), malloc()
math.h Mathematics functions sin(), cos(), pow(), sqrt()
string.h String functions strcpy(), strlen(), strcat()
ctype.h Character handling functions isalpha(), isupper(), ispunct()
time.h Date and time functions asctime(), gmtime(), mktime()
float.h Limits of float types FLT_ROUNDS, FLT_RADIX,
limits.h Size of basic types CHAR_BIT, CHAR_MIN, CHAR_MAX
wctype.h Functions to determine the type contained in wide character data. iswalpha(), iswctype(),iswupper()

Example

A few of the library functions from some header files are used in the code below −

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main() {

   char s1[20] = "53875";
   char s2[10] = "Hello";
   char s3[10] = "World";

   int res;
   res = pow(8, 4);
   printf("Using math.h, The value is : %d\n", res);

   long int a = atol(s1);
   printf("Using stdlib.h, the string to long int: %d\n", a);

   strcpy(s2, s3);
   printf("Using string.h, the strings s2 and s3: %s\t%s\n", s2, s3);

   return 0;
}

Output

When you run this code, it will produce the following output −

Using math.h, The value is: 4096
Using stdlib.h, the string to long int: 53875
Using string.h, the strings s2 and s3: World	World

User-defined Header Files

We can add one or more user-defined functions (apart from the main() function) in the C program. However, if the code consists of a large number of function definitions, putting them in a single source code file with ".c" extension becomes difficult to handle and maintain. Hence, functions/macros/variables of similar nature are clubbed together in header files, and included as we include the standard header files, and call the functions defined in them.

The user-defined header files are usually placed in the same directory in which the C source code is present.

The procedure to create and use a header file with CodeBlocks IDE has been described below. Start CodeBlocks IDE and create a new Console Application −

Header Files

Choose a suitable name to the project. Add a new empty file in the folder, and save the following code as "myheader.h"

#ifndef MYHEADER_H
#define MYHEADER_H

void sayHello();
int add(int a, int b);
double area(double radius);
int length(char *x);

#endif

If a header file happens to be included twice, the compiler will process its contents twice and it will result in an error. The standard way to prevent this is to enclose the entire real contents of the file in a conditional definition with #ifndef directive, known as a header guard.

The header guard checks whether the header file has been defined or not. If it's not defined, it means the file is being included for the first time, and the code inside the guard will be processed.

The header file contains the forward declarations or the prototypes of the functions to be included. The actual definition of these functions is provided in a ".c" file with the same name as the header file.

Creating a Header File

Save the following code in "myheader.c" file −

#include <stdio.h>
#include <string.h>
#include <math.h>

#define PI 3.142

void sayHello(){
   printf("Hello World\n");
}

int add(int a, int b){
   int result;
   result = a+b;
   return result;
}

double area(double radius){
   double areaofcircle = PI*pow(radius, 2);
   return areaofcircle;
}

int length(char *x){
   return strlen(x);
}

Using the Header File

We can now include the "myheader.h" file in the program and call any of the above functions. Save the following code as "main.c" in the project folder.

#include <stdio.h>
#include "myheader.h"

int main() {

   int p = 10, q = 20;
   double x = 5.25;

   sayHello();
   printf("sum of %d and %d is %d\n", p, q, add(p,q));
   printf("Radius: %lf Area: %lf", x, area(x));

   return 0;
}

Build the project and run "main.c", either from the run menu of CodeBlocks IDE or from the command-line to get the following result −

Hello World
sum of 10 and 20 is 30
Radius: 5.250000 Area: 86.601375

Computed Includes

Sometimes it is necessary to select one of the several different header files to be included into your program. For instance, they might specify configuration parameters to be used on different sorts of operating systems. You could do this with a series of conditionals as follows −

#if SYSTEM_1
   # include "system_1.h"
#elif SYSTEM_2
   # include "system_2.h"
#elif SYSTEM_3
   ...
#endif

But as it grows, it becomes tedious, instead the preprocessor offers the ability to use a macro for the header name. This is called a computed include. Instead of writing a header name as the direct argument of #include, you simply put a macro name there −

#define SYSTEM_H "system_1.h"
...
#include SYSTEM_H

SYSTEM_H will be expanded, and the preprocessor will look for system_1.h as if the #include had been written that way originally. SYSTEM_H could be defined by your Makefile with a -D option.

Advertisements