Every C++ object and expression has a type. The type controls which values can be represented, which operations are valid, how overloads are selected, and often how much storage and alignment are required.
Beginners should choose types from the domain first: counts need a nonnegative range, money needs an exact representation policy, measurements need known precision, and protocol fields may require fixed-width integers. The built-in type sizes are implementation-dependent, so portable code checks limits instead of guessing bytes.
C++ data types describe what kind of value a variable can store and how operations behave. int is commonly used for whole numbers, double for fractional numeric work, char for individual character values, bool for true or false, and std::string for text. Choose a type by meaning, range, and required precision.
C++ is statically typed, so the compiler checks many mistakes before the program runs. This is helpful, but it also means conversions matter. Assigning a double to an int loses the fractional part. Mixing signed and unsigned values can produce surprising comparisons. Beginners should make conversions visible instead of relying on accidental behavior.
Keywords are reserved words with special meaning, such as int, return, if, class, const, auto, namespace, public, private, and template. You cannot use keywords as variable names. Learn keywords in context instead of memorizing a list; they become meaningful as you write declarations, control flow, functions, and classes.
Production C++ often needs explicit width types such as std::int32_t or std::uint64_t when data crosses files, networks, binary formats, or APIs. For ordinary local loop counters and sizes, match the standard library type when needed. Avoid unsigned arithmetic as a general bug-prevention strategy; it can hide underflow problems.
const is a design tool, not only a compiler restriction. Mark values and member functions const when they should not change observable state. This documents intent and allows safer reuse. Use constexpr when a value or function can be evaluated at compile time and that constraint improves clarity.
auto should reduce noise, not hide important meaning. It is excellent for iterators and factory functions with obvious initializers, but explicit types can be clearer for numeric precision, ownership, and public interfaces. Strong types, enum class, and scoped names reduce accidental mixing of unrelated values.
Run the examples on your compiler and compare sizes, limits, conversions, and inferred types.
This example shows type choice and a visible narrowing conversion.
#include <iostream>
#include <string>
int main() {
int lessonsCompleted = 7;
double score = 86.75;
bool passed = score >= 60.0;
std::string name = "C++ learner";
int roundedDown = static_cast<int>(score);
std::cout << name << " completed " << lessonsCompleted << " lessons.\n";
std::cout << "Score as int: " << roundedDown << ", passed: " << passed << '\n';
}
Scoped enums and const functions reduce accidental misuse.
#include <iostream>
enum class UserRole {
Student,
Instructor,
Admin
};
class User {
public:
explicit User(UserRole role) : role_(role) {}
bool canEditCourse() const {
return role_ == UserRole::Instructor || role_ == UserRole::Admin;
}
private:
UserRole role_;
};
The usual arithmetic conversions can turn the negative <code>int</code> into a large unsigned number before comparison. This makes expressions involving <code>vector.size()</code> especially surprising.
Use a fixed-width integer when a binary file, network protocol, ABI, or hardware register requires an exact width. For ordinary counters and calculations, <code>int</code> is often clearer and naturally suited to the platform.
Converting a floating-point value to an integer discards the fractional part; it does not round. Likewise, <code>5 / 2</code> performs integer division before any later assignment to <code>double</code>. Make one operand floating point and use an explicit rounding function when truncation is not the intended rule.
Explore 500+ free tutorials across 20+ languages and frameworks.