Friday, July 29, 2016

C Basics - Header Files in C

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files.

A simple practice in 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.

There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

Include Syntax:
Header files are included in source files using the preprocessing directive #include. It has the following two forms.
#include <file>
This form is used for system header files. It searches for a file named 'file' in a standard list of system directories.

#include "file" 
This form is used for header files of your own program. It searches for a file named 'file' in the directory containing the current source file.

You can prepend directories to this list with the -I option while compiling your source code.

Include Operation:
The #include directive directs the preprocessor to scan specified file and copy the content of the header file in to source file.

Only Once Include:
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 using conditional directive in header file.

the entire header file file


Computed Include:
It may be required to select one of the several different header files to be included into your program.
#if SYSTEM_1
   # include "system_1.h"
#elif SYSTEM_2
   # include "system_2.h"
#elif SYSTEM_3

The preprocessor offers the ability to use a macro for the header name. This is called a computed include.
#define SYSTEM_H "system_1.h"
#include SYSTEM_H

ANSI C Standard Library Header Files
assert.h Diagnostics
ctype.h Character Class Tests
errno.h Error Number
float.h Implementation-defined Floating-Point Limits
limits.h Implementation-defined Limits
locale.h location specific information
math.h Mathematical Functions
setjmp.h Non-local Jumps
signal.h Signals
stdarg.h Variable Argument Lists
stddef.h Standard definitions
stdio.h Input and Output
stdlib.h Utility functions
string.h String functions
time.h Time and Date functions

Related topics:
Standard Input-Output in C   |   File Handling in C   |   Preprocessors in C   |   Type Casting in C   |   Error Handling in C

List of topics: C Programming

No comments:

Post a Comment