Header files in C
Header files in C
In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are shared among several different source files. The C standard consists of many libraries that include many predefined functions that make programming and compiling much easier. The header files are made up of predefined standard library functions.
A programmer or a developer can make use of these header files by requesting the header file and including it with a C preprocessing directive “#include”. By doing so, a compiler can make use of the contents present within the header files in the program. All the extensions must necessarily have ‘.h’ extensions in the C standard.
Type of header files
There are two types of header files such as, the files that the programmer writes and the files that come along with the compiler.
- Pre-existing header files
- User defined header files
- Pre-existing header files
The files that are already present within the C compiler and can be imported any time the developer needs to.
- User defined header files
These files are defined by the user and can be imported using the keyword “#include.”
In the C standard, including the header file is nothing but copying the content of that particular header file into your compiler, but doing so will make it error prone and will not be the best idea to copy the content of the header file in the source files, primarily if the program consists of multiple files in the program. It is best if we keep all of the constants, macros, and other system wide global variables and function prototypes in the header files and include the same file where ever it is required.
In the C program, one should include the header file, which stands for the standard input and output used to take input with the help of scanf() and printf() function, respectively.
Syntax
#include <file> Or #include “file”
Explanation
- Both the user and the system header files are included using the preprocessing #include directive.
- The first form, which has angular brackets, is used for the system header that searches the file name, which is specified inside the angular bracket in the standard list of system directories. Directories can be prepended to this list with the -l option while compiling the code.
- The second form is used within the double inverted commas that searches for a file name that will be specified within the inverted commas. Directories can be prepended to this list with the -l option while compiling the code.
#include directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file of the source.
The output from this preprocessor contains the output already generated output resulting from the included file, followed by the output that comes from the text after the directive. Same header files cannot be included more than once within the same program.
The header file contains the following:
- Function definitions
- Macros
- Data type definitions
In the following table, we will see some of the most commonly used header files in C language:
S.No. | Header files | Explanation |
1 | stdio.h | It defines the input or output functions. |
2 | conio.h | It defines the console input or output functions. |
3 | stdlib.h | It indicated the general utility functions. |
4 | math.h | It defines the mathematic functions. |
5 | string.h | It defines the string functions. |
6 | ctype.h | It refers to the character handling functions. |
7 | time.h | It fetched the date and time for the compiler. |
8 | float.h | It limits the float types. |
9 | limit.h | It limits the basic data types. |
10 | wctype.h | It is necessary for the compiler to determine the type contained in the wide character of the data. |
11 | assert.h | Conditionally compiled the macro that compares the argument to a zero. |
12 | complex.h | It reflects the number system that is complex. |
13 | errno.h | As the name suggests, it is a macro that reports the error condition present. |
14 | fenv.h | It represents the floating-point environment. |
15 | inttypes.h | It converts the format into integer types. |
16 | locale.h | It provides with localization facilities. |
17 | wchar.h | It extends the multibyte and widens the character utilities. |
Example
#include<stdlib.h> #include <conio.h> #include<string.h> #include<math.h> int main() { char s1[20] = "112"; char s2[10] = "Tutorials"; char s3[20] = "and Examples"; 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); printf("Using string.h, the strings s2 and s3 : %s %s\n", s2, s3 ); return 0; }
Output
C programming should be well included with the header files, and the programmer developing an application must be well acquainted with its usage. In this way, developing a program will be much easier.