×

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
  1. 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 filesExplanation
 1stdio.hIt defines the input or output functions.
 2conio.hIt defines the console input or output functions.
 3stdlib.hIt indicated the general utility functions.
 4math.hIt defines the mathematic functions.
 5string.hIt defines the string functions.
 6ctype.hIt refers to the character handling functions.
 7time.hIt fetched the date and time for the compiler.
 8float.hIt limits the float types.
 9limit.hIt limits the basic data types.
 10wctype.hIt is necessary for the compiler to determine the type contained in the wide character of the data.
 11assert.hConditionally compiled the macro that compares the argument to a zero.
 12complex.hIt reflects the number system that is complex.
 13errno.hAs the name suggests, it is a macro that reports the error condition present.
 14fenv.hIt represents the floating-point environment.
 15inttypes.hIt converts the format into integer types.
 16locale.hIt provides with localization facilities.
 17wchar.hIt 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

Header Files In C

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.


Related Topics

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Prime Number Program in C using for Loop

In this article, we will know about the procedure of checking whether a natural number inputted by the user is a prime number or non-prime number. Definition of the prime number A...

3 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

How to initialize array to zero in C

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.