Module 1 · Getting Started with C++ — topic 3 of 3
Basic Syntax and Program Structure
Headers, the std namespace, statements and blocks, comments, input with std::cin and output with std::cout, and the rules for identifiers and keywords.
Learning objectives
- Read and write the basic structure of a C++ program
- Use std::cin and std::cout for input and output
- Use using-declarations responsibly
Definition
A C++ program is a set of declarations and definitions spread over translation units; execution begins in the global functionmain. Standard library facilities are made available with#includeand live in namespacestd.
Introduction
C++ syntax looks like C: statements end with semicolons, blocks use braces, and comments use // or /* */. The differences appear in how you do input/output and organise names.
Instead of printf and scanf, C++ uses streams. std::cin >> x reads a value of whatever type x is; std::cout << x prints it. The compiler picks the right conversion from the type, so there are no format specifiers to get wrong.
Why it matters
Type-safe streams remove an entire class of C bugs (wrong %d/%lf).
Understanding namespaces explains the std:: prefix you will see everywhere and why using namespace std; in headers is discouraged.
Detailed explanation
#include <iostream> brings in std::cin, std::cout and std::cerr. #include <string> brings in std::string.
std::cin >> a >> b; reads whitespace-separated values. After a failed read, std::cin enters a fail state; test it with if (std::cin >> x) or if (!std::cin).
std::getline(std::cin, line) reads a whole line including spaces. Mixing >> and getline requires discarding the leftover newline with std::cin.ignore() or std::ws.
A using-declaration such as using std::cout; imports one name. using namespace std; imports every name in std; it is acceptable in small .cpp examples but should never appear in a header, where it would affect every file that includes it.
Syntax
>>extracts from an input stream;<<inserts into an output stream.std::stringgrows automatically; no buffer size is needed.- Reaching the end of
mainreturns 0 automatically.
Code examples
Reading two values
12 30
12 + 30 = 42
Reading a full line
New Delhi
[New Delhi] has 9 characters
More examples
Validating input and mixing >> with getline
42 Asha Rao
Roll: Name: 42: Asha Rao
Step-by-step explanation
if (!(std::cin >> roll))checks that an integer was actually read.- After reading 42 the newline is still in the input.
std::cin >> std::wsdiscards whitespace, sogetlinereads "Asha Rao" rather than an empty line.- The final line prints both values.
Expected output
Running “Validating input and mixing >> with getline” with the sample input shown prints exactly:
Practical applications
- Command-line tools read configuration with streams.
- Competitive programming solutions read input with
cin >>. - Log output goes to
std::cerrso it isn't mixed with normal output.
Best practices
- Check stream state after reading input.
- Prefer
std::stringandstd::getlinefor text. - Keep
using namespace std;out of headers.
Common mistakes
getline after >> returns an empty string
Avoid
Prefer
>> leaves the newline in the stream; getline stops at it immediately.
using namespace std in a header
Avoid
Prefer
It pollutes the namespace of every file that includes the header and can cause name clashes.
Important notes
- For fast input in competitive programming,
std::ios::sync_with_stdio(false); std::cin.tie(nullptr);speeds up streams. - C++23 adds
std::printandstd::println, which use format strings like Python.
Key takeaways
- Streams:
std::cin >>reads,std::cout <<prints. - Use
getlinefor lines andstd::wsto skip leftover newlines. - Names in the standard library are in namespace
std.
Try it yourself
- 1Read a full name and a CGPA, then print "<name> has CGPA <cgpa>" with two decimals using
<iomanip>'sstd::fixedandstd::setprecision(2). - 2Keep reading integers until the input ends and print their sum.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
4 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerIn `std::cout`, what does `std::` mean?
- BeginnerGiven the input `12 30`, what is printed?
- BeginnerAfter `std::cin >> age;`, a following `std::getline(std::cin, name);` returns an empty string. Why?