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
| Header | Gives you | Used for |
|---|---|---|
| <iostream> | cout, cin, endl | Screen output, keyboard input |
| <cstring> | strlen, strcpy, strcat, strcmp | C-string handling |
| <cmath> | sqrt, pow, abs, floor | Mathematics |
| <cstdlib> | rand, atoi, exit | Utilities, conversions |
| <iomanip> | setw, setprecision | Formatting output columns |
| <fstream> | ifstream, ofstream | Reading, 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?
- <cmath> and <iomanip>
- <iostream> and <cstring>
- <cmath> and <cstdlib>
- <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.