Getting Started with C++

Header Files

Language has various libraries which contain predefined functions for making programming simpler. In C language, header files comprise the set of the predefined standard library function. Similarly, C++ also provides its users with a range of functions including header files. In this language, all the header files may or may not end with the ‘.h’ extension. However, in C, all of these files do end with a ‘.h’ extension.

header files

Composition of Header File

A header file contains:

  • Function definitions
  • Data type definitions
  • Macros

Further, it provides the users with the features given above by importing them into the program with the assistance of a preprocessor directive “#include”. Further, these preprocessor directives come into use for instructing the compiler that these files are required to be processed before compilation.

Moreover, the C program must essentially include the header file which stands for standard input and output utilized to take input with the aid of scanf() and printf() function correspondingly.

Further, the C++ program has the header file. Also, it stands for input and output stream which comes in use to take input with the help of “cin” and “cout” respectively.

Types of Header Files

There are two kinds of header files:

Pre-existing header files

They are files that are already available in the C/C++ compiler. Thus, they just need to be imported.

User-defined header files

These files are defined by the user and we can import them using “#include”.

Syntax:

#include <filename.h>

or

#include “filename.h”

One can include header files in their program by making use of one of the above two syntaxes. Also, it may be either a pre-defined or user-defined header file. Thus, the “#include” preprocessor directs the compiler that the header file is required to be processed before compilation.

Moreover, it contains all the essential data type and function definitions. Most importantly, one cannot include the same header file twice in any program.

Browse more Topics Under Getting Started with C++

Creating Your Own Header File

You can create your own header file and include them in your program to use it whenever suitable instead of writing a large and complex code. It will improve code functionality as well as readability. Follow the step below for creating your own header file:

  1. Write your own C/C++ code and save that file with “.h” extension. Check out the illustration of the header file below:
                    

// Function to find the sum of two

// numbers passed

int sumOfTwoNumbers(int a, int b)

{

return (a + b);

}

Include your header file with “#include” in your C++ program like the following:

                    

// C++ program to find the sum of two

// numbers using function declared in

// header file

#include "iostream"

// Including header file

#include "sum.h"

using namespace std;

// Driver Code

int main()

{

// Given two numbers

int a = 13, b = 22;

// Function declared in header

// file to find the sum

cout << "Sum is: "

<< sumOfTwoNumbers(a, b)

<< endl;

}

 

Output:

                    

Sum is: 35

Process returned 0 (0*0) execution time: 0.287 s

Press any key to continue

Uses of Standard Header Files

#include<stdio.h>: For performing input and output operations by making use of functions scanf() and printf().

#include<iostream>: A stream of Input and Output by making use of cin and cout.

#include<string.h>: Performing several functionalities related to string manupulation like strlen(), strcmp(), strcpy(), size(), etc.

#include<math.h>: Performing mathematical operations such as sqrt(), log2(), pow(), etc.

#include<iomanip.h>: Accessing set() and setprecision() function for limiting the decimal places in variables.

#include<signal.h>: Performing signal handling functions such as signal() and raise().

#include<stdarg.h>: Performing standard argument functions such as va_start() and va_arg().Useful for indicating the start of the variable-length argument list and fetching the arguments from the variable-length argument list in the program respectively.

#include<errno.h>: Performing error handling operations like errno(), strerror(), perror(), etc.

#include<fstream.h>: Controlling the data to read from a file as an input and data for writing into the file as an output.

#include<time.h>: Performing functions related to date() and time() such as setdate() and getdate(). Also for modifying the system date and get the CPU time respectively.

#include<float.h>: Includes a set of various platform-dependent constants related to floating point values. Moreover, these constants are proposed by ANSI C. Thus, they enable to make the programs more portable. For instance, some constants included in this header file are- e(exponent), b(base/radix), etc.

#include<limits.h>: Determining several properties of the different variable types. , the macros defined in this header is responsible for limiting the values of different variable types such as char, int, and long. Further, these limits are what specifies a variable can’t store any value beyond these limits. For instance, an unsigned character can store up to a maximum value of 255.

FAQ on Header Files

Question 1: What is header file in C++ with example?

Answer 1: A header file refers to a file with extension .h that contains C function declarations and macro definitions which are to be shared between multiple source files. Further, there are two kinds of header files. Firstly, the files that the programmer writes. Secondly, the files that come with your compiler.

Question 2: How many header files are there in C++?

Answer 2: There are 49 header files in the Standard C++ Library. Further, his contains equivalents of the 19 Standard C Library header files. Moreover, all of the equivalent C header files have a ‘c’ prepended to the name and have no . h file extension.

Question 3: What is #include in C?

Answer 3: The #include directive tells the C preprocessor to include the contents of the file specified in the input stream to the compiler. After that, continue with the rest of the original file. Similarly, a header file may contain any valid C program fragment.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.