C Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Header Files in C

Header Files in C

shape Description

The file which consists of macro definitions and C-function declarations is called as header file.
  • The extension for header files in C is .h
  • '.h' extension file can be used by many of the source file programs written the programmer before the source files are compiled.
  • The header files can be used anywhere in the source files by using pre-processor directive #include, in which #symbol implies that the declared statement is pre-processor pointing to a particular file but not normal C-language statement.
  • The header files helps in saving the time and complexity in finding a specific file.
Here, the doubt is that, instead of declaring the header file, just copy and paste the statements. But that process is not suggested because it results in errors.

System in-built header file

 

shape Description

Built-in header files are given within angular braces < >.

shape Syntax

#include<file_name>

shape More Info

The file name given is searched in the group of system directories and this is referred as search path.These are only readable files and writing data is not possible in built-in files.There are many header files in compiler. Some of the header files in C-standard libraries are: 

shape Header files

Name Description
stdio.h Input/output functions
conio.h Console input/output
stdlib.h General Utility functions
string.h String functions
math.h Mathematics functions
stdarg.h Variable Argument list functions
signal.h Signal Handling functions
setjmp.h Non-local jump functions
ctype.h Character handling functions
locale.h Localization functions
time.h Date and Time functions
assert.h Diagnostics functions
stdbool.h and stddef.h Boolean data type functions, useful types and Macro definitions

User-defined header files

shape Introduction

Till now, only single program with .c extension was seen. But, in real time projects, there will be multiple .c files with functions spread over all these files.

shape Description

User-defined header files are represented within double qoutes (" ") and are given by the user. User-defined header files are mostly used when doing a project. The programs under source files are stored with '.c' extension and the source file may contain many programs with '.c' extensions. All these .c extension files are compiled and linked to form a single .exe extension file such that all the files included uses the header files.

shape Syntax

#include"file_name.h"

shape Examples

For example, create a file with .h (say splessons.h) extension and write the following code: [c] int Add(int a, int b) { return a+b; } int sub(int a,int b) { return a-b; }[/c] Now create a file with '.c' (say itoolsinfo.c) extension and write the following code: [c] #include<stdio.h> #include"splessons.h" int main() { int x,y,sum; x=1; y=2; sum=Add(x,y); printf(%d", sum); return 0; }[/c] In the above program, there is no need to declare that x and y are integer variables in the calling function as already stated that the variables are integers using header files.In this way, the user-defined header files are used.

shape Alternative forms

There is also an other case to include the header files multiple times which will result in an error. To avoid this, include the declarations of header file in #ifndef, #define and #endif. For example: [c] #ifndef splessons int Add(int a, int b) int Sub(int a, int b) #define splessons #endif[/c] By using the above syntax in the header file, the entire contents get skipped as the header files are already defined.

Summary

shape Key Points

  • Header files in C has extension ".h".
  • Included in the program using pre-processor directive "#include"
  • Built-in header files and User-defined header files are the types of header files.

shape Programming Tips

  • Without header file, program cannot be executed and this must be placed at the top of the program.
  • Do not give space between # and include.