Header files in C

What are the Header Files?

Header files in C language offers numerous libraries that include predefined functions to make programming easier. By using C predefined directive ‘#include’, programmers can use the functions defined in the header file. All the header files have ‘.h’ extension.

The default header file that comes with the C compiler is stdio.h. We can’t include same header files twice in any program.

A header file contains following parts.

  • Function Definitions
  • Data type definitions
  • Macros

There are two types of header files. They are as follows

  • Pre-existing Header files: Files which are already defined in C compiler.
  • User-defined Header files: These files are defined by user.

Syntax

#include<filename.h>
      OR
#include "filename.h"

Example program

// C program to illustrate header files
#include<stdio.h>
int main()
{
  printf("Hello, this is "Schlarsoul" \n);
  return 0;
}

Code explanation

In the above program, stdio.h header file is included in order to use printf().

Output

Header files in C Example
Header files in C

Including multiple Header files

If in case when a header file need to be included twice within the program, the compiler is going to process it twice that will lead to an error. In order to overcome this issue, one has to use conditional preprocessor directives. It’s syntax is as follows.

#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME
  The entire header file
#endif

Sometimes based on the requirement of the program, one has to use multiple conditionals.

#if FIRST_SYSTEM
  #include "sys1.h"
#elif SECOND_SYSTEM
  #include "sys2.h"
  ...
#endif


C Standard header files and their uses

  • stdio.h: It is used to perform input/output operations using printf() and scanf().
  • string.h: It is used to perform various functionalities related to string manipulation like strcmp(),strlen(), etc.
  • math.h: It is used to perform various mathematical operations like sqrt(),pow(),etc.
  • errno.h: It is used to perform error handling operations like errno(),perror(),etc.
  • time.h: It is used to perform functionalities related to date() and time() like setdate() and getdate().
  • assert.h: Conditionally compiled macro that compares its argument to zero.
  • complex.h: Complex number arithmetic.
  • ctype.h: Functions to determine the type contained in character data.
  • fenv.h: Floating-point environment.
  • float.h: Limits of float types.
  • inttypes.h: Format conversion of integer types.
  • iso646.h: Alternative operator spellings.
  • limits.h: Size of basic types.
  • locale.h: Localization utilities.
  • setjmp.h: Nonlocal jumps.
  • signal.h: Signal handling.
  • stdalign.h: aligns and align of conveniance of macros.
  • stdarg.h: Variables arguments.
  • stdatomic.h: Atomic types.
  • stdbool.h: Macros for boolean type.
  • stddef.h: Common macro definitions.
  • stdint.h: Fixed-width integer types.
  • stdnoreturn.h: Nonreturn conveninence macros.
  • tgmath.h: Type-generic math.
  • threads.h: Thread library.
  • uchar.h: Extended multibyte and wide character utilities.
  • wchar.h: Extended multibyte and wide character utilities.
  • wctype.h: Functions to determine the type contained in wide character data.

People also ask for

What is the role of header file in C?

All C standard library functions are declared in many header files. To use those functions in your program, you need to include those files in your program by using preprocessor directive ‘#include’.