What is a header file?

A file with an extension that has some predefined declarations and definitions is called a header file. Every computer program imports a set of header files to perform common tasks such as getting input, displaying output, and so forth. Every language has different header files (such as C, JAVA, Python, and C++). In this article, header files are explained mainly in the context of the C/C++ language.

Components of a header file

Any header file has 3 components:

1. Function definition: Function definition corresponds to the body of the function. The header file consists of macro definitions corresponding to standard library functions. The parameters are replaceable and contains an identifier with a parameter list.

2. Data type definition: The declaration of header files tells the compiler whether the element is an int, a double, a function or another.

3. Macro: It may be a piece of code in a program that is replaced by the value of the macro. All preprocessing directives begin with a # symbol such as #define PI 3.14.

Types of header files

 The two types of header files are:

  • Pre-defined header file: Files that already exist in the compiler are called pre-defined header files.
  • User-defined header file: Files defined by the user itself are called user-defined header files.


#include <filename.h>


#include “filename.h”

Working of header files

The source file contains #include, which is a command that instructs the compiler to process the files before compilation. It includes all the mandatory data types and function definitions. Angular brackets (<>) tells the compiler to look for a definition in specific paths where the headers are stored by default to compile the program. There is a total of 49 header files in the standard C++ library. In addition, it includes 19 standard C library header files.

Commonly used header files

Below are some important header files along with their usage.

  • #include<conio.h>: It includes all the console input and output functions.
  • #include<stdlib.h>: It includes standard library functions like malloc(), calloc(), exit(), involved in memory allocation, process control, and conversions.
  • #include<ctype.h>: It includes all character manipulation functions such as isalpha(), islower(), isdigit().
  • #include<stdio.h>: It defines input and output operations in a program through the scanf() and printf() functions.
  • #include<iostream.h>: It defines functions for performing input and output operations. It includes stream objects like cout and cin for giving output and taking input.
  • #include<string.h>: It includes many functionalities associated with string manipulations such as strlen(), strcmp(), strcpy(), size(), and so on.
  • #include<math.h>: It defines the mathematical operations such as sqrt(), log2(), pow(), and so on.
  • #include<iomanip.h>: It includes manipulators such as set() and setprecision() performed to limit the decimal places in variables.
  • #include<signal.h>: It includes signal handling functions such as signal() and raise().
  • #include<stdarg.h>: It defines the common place argument functions such as va_start () and va_arg(). It indicates the beginning of the variable-length argument list and takes the arguments from the variable-length argument list within the program.
  • #include<errno.h>: It includes error handling operations such as errno(), strerror(), perror(), and so forth.
  • #include<time.h>: It defines the functions associated with the date() and time() such as setdate(), getdate() and so forth.
  • #include<float.h>: Includes a collection of assorted platform-dependent constants associated with floating purpose values. Moreover, these constants square measures are planned by ANSI C. For instance, some constants enclosed during this header file are e (exponent), b (base/ radix), and so on.
  • #include<limits.h>: It includes the functions that determine many properties of various variable types where the macros outlined during this header, limit the values of a variable such as char, int, or long. Further, these limit the variable from storing any value from the specified limits. For instance, an unsigned character stores up to 255 places.

Function declaration

A block of code, which is called from another location in a C program (or any other high-level language program) or class, is known as the declaration of a function.

Function prototype

Function prototype contains the declaration of a function that includes the name of parameters, the data type of parameters and the return type of the function. It does not include the body of the function. The name of the parameter is not a necessity whereas the type of data is compulsory. It focuses on the interface and flow of input and output data. A program with function prototype declaration can be divided into translation units by the compiler.

Compilation process

The compiler is used to compile a computer program. The source code is converted into machine code by the compiler. The compilation process of a computer program consists of four steps.

(i) Preprocessing

The preprocessor processes the program by removing comments, expanding macros, and expanding self-enclosed files. The file extension of .c is given to files that are passed to the preprocessor.

(ii) Compiling

Compiling takes the output of the preprocessor and generates assembly programing language. The preprocessed code is changed to assembly code by passing the file to a compiler.

(iii) Assembly

The assembler converts the assembly code into pure binary code or machine code. The file extension .obj is given to the assembly code file.

(iv) Linking

Linking is the ultimate step of compilation. It is done using a linker. The linker merges all the object code from multiple modules into one. It links the object code with the library code.

In static linking, the linker duplicates all the used library functions to the program file during compilation. In dynamic linking, the library functions are dynamically accessed from an external source in the run time.

The preprocessor and its directives

A preprocessor is a program that processes the code before passing it to the compiler. Preprocessor programs give preprocessing directives that instruct the compiler to preprocess the program before compilation. Preprocessor directives are used in a program to request the use of a header file in the program.

For instance, when the preprocessor directive #include<stdio.h> is written in a program, the program requests the use of the header file <stdio.h>.

Preprocessing directives are preceded by the pound “#” symbol. When a statement begins with #, the preprocessor will execute it. Some commonly used preprocessing directives are #include, #define and #if.

Preprocessor directiveDescriptionExample
#includeThis statement includes another file into the current file. Syntax: #include <path> #include “path”#include stdio.h #include “defs.h”
#defineThis defines a macro throughout the code. A macro is a program segment that is replaced by the macro value. Syntax: #define identifier token-string#define Pi 3.14 #define width 15
#undefThis directive is used to cancel a previously defined macro. Syntax: #undef identifier#undef Pi
#ifThis directive is used when a particular condition needs to be checked before the execution of a program. The preprocessor checks the condition, and if it is true, the code will be executed. Syntax: #if expression #if book_count < 60
#ifdefThis directive adds the source program if a macro is defined. It first checks if the macro is defined. If it defined it will return true. Syntax: #ifdef MACRO#ifdef MESSAGE
#ifndefThis directive includes the source code, if the macro is not defined. Syntax: #ifndef MACRO#ifndef MESSAGE
#elifThis directive is used with #if, #ifdef, #ifndef directives. When the statement evaluates to true, it includes an alternative source code. Syntax: #elif expression#elif AGE <5
#elseThis statement is used with #if, #ifdef, #ifndef, and is executed when the condition of these directives evaluates to false. Syntax: #else#else
#endifThis statement is used to end the #if, #ifdef, and #ifndef code blocks. Syntax: #endif#endif
#errorThis directive returns an error message when a fatal error is encountered and stops preprocessing. Syntax: #error token_string#error C compiler required
#pragmaThis directive provides additional information to the complier. It is used to offer operating system features. Syntax: #pragma token#pragma exit func

Context and Application

This topic is significant in the professional exams for undergraduate, graduate, and post-graduate courses

  • Bachelors in Science in Computer Science
  • Masters in Science in Computer Science

Practice Problems

Q1. Which of the following is the correct description of stdio.h header file?

  1. It includes all standard library functions
  2. It includes all input and output functions
  3. It includes all mathematical functions
  4. It contains floating-point functions that return 0.

Answer: Option b

Explanation: stdio.h defines the input and output operations of a program by the use of functions scanf() and printf().

Q2. Which of the following preprocessor directives defines a macro in a program?

  1. #define
  2. #include
  3. #if
  4. #else

Answer: Option a

Explanation: The #define preprocessor directive is used to define a macro in the program.

Q3. Which of the following is used to mark a preprocessor directive?

  1. *
  2. inline functions
  3. int x
  4. #

Answer: Option d

Explanation: # sign is used to mark a preprocessor directive. For instance, #if, #error are some directives.

Q4. Which of the following is the file that is defined by the user itself?

  1. User-defined header
  2. Predefined header
  3. error-prone header
  4. function declarations

Answer: Option a

Explanation: Files that are defined by the user itself are called user-defined header files.

Q5. Which of the following header file contains setprecision()?

  1. console.h
  2. stdlib.h
  3. conio.h
  4. iomanip.h

Answer Option d

Explanation: The header file iomanip.h is used for correcting precision values. It contains manipulators like set() and setprecision().

Common Mistakes

Mistakes in header files will affect the output of a program. Common mistakes include using uncompilable header files that can cause compiler errors. A header file made with unrelated function definitions will not be efficient. The syntax errors and multiple inclusions should be avoided to create a functional header file.

  • C++ Standard Library
  • Java Server Pages
  • Input/output
  • C++ Classes
  • Preprocessor
  • Code file and source file

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science


Fundamentals of Programming

Header Files

Header files Homework Questions from Fellow Students

Browse our recently answered Header files homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science


Fundamentals of Programming

Header Files