C - SPLessons

Chapter 5

Header Files in C

Header Files in C

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

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

#include<file_name>
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: 

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

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.
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.

#include"file_name.h"
For example, create a file with .h (say splessons.h) extension and write the following code:

int Add(int a, int b)
{
  return a+b;
}
int sub(int a,int b)
{
  return a-b;
}

Now create a file with ‘.c’ (say itoolsinfo.c) extension and write the following code:

#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;
}

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.

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:

#ifndef splessons
int Add(int a, int b)
int Sub(int a, int b)
#define splessons
#endif

By using the above syntax in the header file, the entire contents get skipped as the header files are already defined.

Summary

  • 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.

  • 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.