Lesson content
Read, practise, then check your understanding
C++ fundamental types include bool, character types, signed and unsigned integers, and floating-point types. Exact widths depend on the implementation; <cstdint> provides types such as std::int32_t when available. sizeof reports storage in bytes, while std::numeric_limits<T> describes a type’s range and precision.
Initialize every value
#include <cstdint>
#include <string>
std::int64_t population{1'428'000'000};
double ratio{0.625};
char grade{'A'};
bool active{true};
std::string language{"C++"};
Brace initialization is explicit and rejects many narrowing conversions. An uninitialized automatic fundamental value is indeterminate; reading it is erroneous or undefined depending on the language version and context.
const, constexpr, and auto
const prevents modification through that name. constexpr asks for a value or function usable during constant evaluation when its inputs permit it. consteval requires compile-time evaluation. auto deduces a type from an initializer but does not make a value dynamically typed.
constexpr int secondsPerHour{60 * 60};
const auto label = std::string{"runtime value"};
static_assert(secondsPerHour == 3600);
Conversions and scope
Implicit numeric conversions can lose range, sign, or precision. Use a named cast such as static_cast<double>(count) when a conversion is intentional. Keep variables in the smallest useful scope, use meaningful names, and prefer domain types or enumerations when plain numbers could be confused. Signed overflow is undefined behavior; unsigned arithmetic wraps modulo its range, which is useful only when intentional.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.