Syntax, Data Types, Variables, Input Output, and Operators
Learn the basic building blocks of C++ programs including variables, built-in types, expressions, console I/O, and operator behavior.
Inside this chapter
- Basic Syntax and Variables
- Common Built-In Types
- Console Input and Output
- Operators and Expressions
- Type Conversion
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C++ basics to modern ownership, templates, concurrency, performance, and production-ready engineering practices. Use the navigation at the bottom to move smoothly through the full series.
Basic Syntax and Variables
#include <iostream>
int main() {
int age = 21;
double salary = 45000.50;
char grade = 'A';
bool active = true;
std::cout << age << '\n';
}
C++ is strongly typed and case-sensitive. Choosing meaningful variable names and correct types is one of the first important habits students should build.
Common Built-In Types
| Type | Typical Usage |
|---|---|
| int | Whole numbers |
| double | Floating-point values |
| char | Single character |
| bool | True or false conditions |
| std::string | Text data |
Console Input and Output
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter name: ";
std::cin >> name;
std::cout << "Hello, " << name << '\n';
}
std::cin and std::cout are beginner-friendly standard stream interfaces. Later, students should also understand stream states, buffering, and line-based input.
Operators and Expressions
C++ supports arithmetic, relational, logical, assignment, bitwise, and increment-decrement operators. Understanding precedence and side effects is important for correctness and readability.
int a = 10;
int b = 3;
int sum = a + b;
bool bigger = a > b;
int remainder = a % b; Type Conversion
double result = static_cast<double>(5) / 2;
C++ allows both implicit and explicit conversions. Beginners should learn to use explicit casts carefully when intent matters, instead of depending blindly on automatic conversion.
Real-World Usage Snapshot
Every larger C++ system still depends on clean handling of types, input-output, and expressions. Sloppy basics create harder bugs later in templates, containers, concurrency, and performance-critical logic.