×

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

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

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.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Memory leak in C

What is memory leak in C? Memory leak occurs when we keep allocating memory in the heap without freeing it, i.e., the allocated memory in heap is not released back to...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.