Header Files in C++
In this lesson: Standard C++ headers, creating your own .h files and include guards / #pragma once — with examples. In Hindi & English.
What is a header file?
A header file holds *declarations* that more than one part of a program needs: function prototypes, class definitions, constants and templates. When you write #include <iostream>, the preprocessor copies the whole text of that header into your file before the compiler sees it. That is how the compiler knows what std::cout is.
.cpp) = the "kitchen" (how it actually works).Two ways to include
#include <vector> // angle brackets: search the standard/system folders #include "student.h" // quotes: search your project folder first
Use < > for standard library and installed library headers, and " " for headers you wrote.
Most-used standard headers
| Header | What it gives you |
|---|---|
<iostream> | cin, cout, cerr, endl |
<iomanip> | setw, setprecision, fixed for formatted output |
<string> | std::string and its functions |
<vector> | std::vector dynamic array |
<cmath> | sqrt, pow, abs, sin, ceil, floor |
<algorithm> | sort, find, max_element, reverse |
<fstream> | ifstream, ofstream for files |
<map>, <set> | std::map, std::set |
<memory> | unique_ptr, shared_ptr, make_unique |
<cstdlib> | rand, exit, abs (from C) |
<ctime> | time — often used with srand |
C library headers have a C++ version with a c prefix and no .h: use <cmath> instead of <math.h>, <cstring> instead of <string.h>.
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
double r = 2.5;
std::cout << std::fixed << std::setprecision(2)
<< "Area = " << M_PI * std::pow(r, 2) << "\n"
<< "sqrt(2) = " << std::sqrt(2.0) << "\n";
return 0;
}(M_PI is provided by GCC/Clang's <cmath>; on MSVC define _USE_MATH_DEFINES first or use your own constexpr double PI.)
Writing your own header
Split a program into three files: the header declares, one .cpp defines, and main.cpp uses.
// student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
class Student {
public:
Student(std::string name, int marks);
void show() const;
bool passed() const;
private:
std::string name_;
int marks_;
};
#endif // STUDENT_H// student.cpp
#include "student.h"
#include <iostream>
Student::Student(std::string name, int marks)
: name_(name), marks_(marks) {}
void Student::show() const {
std::cout << name_ << ": " << marks_
<< (passed() ? " (Pass)" : " (Fail)") << "\n";
}
bool Student::passed() const { return marks_ >= 33; }// main.cpp
#include "student.h"
int main() {
Student a("Aman", 78), b("Ravi", 29);
a.show();
b.show();
return 0;
}g++ main.cpp student.cpp -o result ./result
Only .cpp files go on the compile command; headers are pulled in by #include.
Include guards and #pragma once
If student.h gets included twice (directly and through another header), class Student would be defined twice — an error. The #ifndef / #define / #endif lines above prevent that: the second time, STUDENT_H is already defined, so the content is skipped.
Most compilers also accept a one-line alternative at the top of the header:
#pragma once
It is not part of the official standard but is supported by GCC, Clang and MSVC. Either approach is fine; just use one in every header you write.
What goes where
| Put in the header (.h) | Put in the source (.cpp) |
|---|---|
| Class definitions | Member function bodies |
| Function prototypes | Function bodies |
constexpr/const constants | Global variable definitions (if any) |
Templates and inline functions (whole code) | Helper functions used only in that file |
| Include guards | #include of headers needed only for the implementation |
iostream.h, conio.h and bits/stdc++.h
<iostream.h>and<conio.h>(clrscr(),getch()) come from Turbo C++. They do not exist in standard C++ — use<iostream>and dropclrscr()/getch().<bits/stdc++.h>includes the entire library. It works only on GCC and slows compilation; fine in competitive programming, but avoid it in school projects and real code.
Common mistakes
- Writing
#include <iostream.h>or#include "iostream"for standard headers. - Forgetting include guards and getting "redefinition of class" errors.
- Defining a normal function in a header included by two
.cppfiles — "multiple definition" at link time. - Putting
using namespace std;in a header, which leaks into every file that includes it. - Compiling only
main.cppand getting "undefined reference" becausestudent.cppwas not compiled.
Create mathutils.h with prototypes for int factorial(int) and bool isPrime(int), implement them in mathutils.cpp, and use both from main.cpp. Add include guards, then include the header twice in main.cpp to confirm the guard works.
Summary
- Headers contain declarations;
#includecopies them in before compilation. < >for standard/library headers," "for your own.- Use
<cmath>-style C++ headers, never<iostream.h>. - Protect every header with include guards or
#pragma once. - Declarations in
.h, definitions in.cpp; templates and inline functions stay in the header.
Related: preprocessor directives, namespaces, structure of a C++ program.
Frequently Asked Questions
What is a header file in C++?
.h or .hpp, or no extension for standard headers like <iostream>) contains declarations — function prototypes, class definitions, constants and templates — that several source files need. #include copies its text into your file before compilation so the compiler knows those names.What is the difference between and ?
<iostream> is the standard C++ header and puts its names in namespace std. <iostream.h> is a pre-standard header from old compilers like Turbo C++; modern compilers such as GCC, Clang and MSVC do not provide it, so always use <iostream>.What are include guards and why are they needed?
#ifndef NAME_H / #define NAME_H / ... / #endif) or #pragma once make sure the header's content is used only once.Should I put function definitions in a header file?
.cpp file. Exceptions are templates, inline functions and constexpr functions, which normally live entirely in the header. Putting ordinary function definitions in a header that is included in several .cpp files causes "multiple definition" linker errors.