Various library (header) files required for C++

Header files hand your program ready-made declarations (cout, strlen, sqrt) through one #include line, so you build on the standard library instead of rewriting it.

8 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Four words that stop your program

You type the classic first line of the canteen till:

cout << "Welcome to CanteenTill";

The compiler refuses: 'cout' was not declared in this scope.

Nothing is wrong with your logic. The compiler simply has never heard of cout. Someone has to introduce them, and that introduction is one line: #include <iostream>.

Theory

The supplier catalogue

The canteen does not grow its own tea leaves. It keeps supplier catalogues: order from the beverage catalogue, the grain catalogue, the packaging catalogue.

Header files are your program's catalogues. iostream is the input/output catalogue, cmath the mathematics catalogue. #include puts that catalogue on your desk so the compiler knows every item you might order from it.

Theory

What a header file actually is

A header file contains declarations: the names and signatures of ready-made functions, objects and classes from the standard library.

#include is a preprocessor directive: before compilation begins, the preprocessor literally pastes the header's contents into your file. That is why it starts with # and takes no semicolon.

Declarations tell the compiler what exists; the compiled library code linked later provides how it works.

At a glance

The headers BCA304 programs actually use

HeaderGives youUsed for
<iostream>cout, cin, endlScreen output, keyboard input
<cstring>strlen, strcpy, strcat, strcmpC-string handling
<cmath>sqrt, pow, abs, floorMathematics
<cstdlib>rand, atoi, exitUtilities, conversions
<iomanip>setw, setprecisionFormatting output columns
<fstream>ifstream, ofstreamReading, writing files

Theory

Angle brackets vs quotes, .h vs no .h

#include <iostream> with angle brackets: the compiler searches its own system directories. Use this for standard library headers.

#include "myitems.h" with quotes: the compiler searches your current folder first. Use this for headers you wrote yourself.

Old C headers get a C++ spelling: string.h becomes <cstring>, math.h becomes <cmath>, stdlib.h becomes <cstdlib>. Both spellings work in exams, but name the modern one when asked.

Quiz

The till program computes sqrt(payment) for a loyalty formula and prints amounts padded with setw(8). Which pair of headers must it include?

  1. <cmath> and <iomanip>
  2. <iostream> and <cstring>
  3. <cmath> and <cstdlib>
  4. <iomanip> and <fstream>
Show the answer

<cmath> and <iomanip>

sqrt lives in <cmath> and setw lives in <iomanip>. <iostream> is surely also present for cout, but the question asks what these two functions need. <cstring> is for strings, <cstdlib> for rand/atoi, <fstream> for files: real families, wrong catalogues for sqrt and setw.

Think first

Predict the compiler's complaint

A program includes only <iostream>, then calls strlen(name). Before tapping: does it compile, and if not, what will the error say?

Show the answer

It fails with something like 'strlen' was not declared in this scope. Including <iostream> introduces only the input/output family. strlen belongs to <cstring>, a different catalogue. One header never buys you the whole library: each #include opens exactly one family of declarations.

Watch out

The trap: one include to rule them all

Students assume <iostream> unlocks everything because their first programs only ever needed it. Then sqrt or strcmp mysteriously fails.

Rule: every family needs its own include. Also remember using namespace std; is doing quiet work in your programs: without it you must write std::cout. Exams ask where cout is defined; the answer is: in the std namespace, declared in <iostream>.

Theory

Where you will meet them again

Every single BCA304 program starts with <iostream>. The strings lesson coming up leans on <cstring>. File handling later in your course uses <fstream>. And rand() from <cstdlib> powers simulations and games. Learn the table above once and you stop losing easy 2-mark questions about which header a function needs.

Summary

Key takeaways

  • A header file supplies declarations of ready-made library names; #include pastes them in before compilation.
  • #include is a preprocessor directive: starts with #, needs no semicolon.
  • <> searches system directories; "" searches your folder first.
  • Modern spellings: <cstring>, <cmath>, <cstdlib> replace string.h, math.h, stdlib.h.
  • One header = one family: iostream for I/O, cstring for strings, cmath for maths, iomanip for formatting, fstream for files.
  • Memory hook: order from the catalogue, do not grow the tea.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of OOPS

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Various library (header) files required for C++ · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn