C++ - Types & Keywords
Overview
Estimated time: 35–50 minutes
Learn the core builtin types, their sizes and limits, numeric and character literals, and get familiar with commonly used keywords in modern C++.
Learning Objectives
- Identify fundamental types (bool, char variants, integer, floating-point).
- Print sizes and limits using sizeof and numeric_limits.
- Recognize key language keywords grouped by purpose.
Prerequisites
Fundamental types and sizes
#include <iostream>
#include <limits>
int main(){
std::cout << "sizeof(bool)=" << sizeof(bool) << "\n";
std::cout << "sizeof(char)=" << sizeof(char) << "\n";
std::cout << "sizeof(int)=" << sizeof(int) << "\n";
std::cout << "sizeof(long)=" << sizeof(long) << "\n";
std::cout << "sizeof(long long)=" << sizeof(long long) << "\n";
std::cout << "sizeof(float)=" << sizeof(float) << "\n";
std::cout << "sizeof(double)=" << sizeof(double) << "\n";
std::cout << "sizeof(long double)=" << sizeof(long double) << "\n";
}
Expected Output (example on 64-bit):
sizeof(bool)=1
sizeof(char)=1
sizeof(int)=4
sizeof(long)=8
sizeof(long long)=8
sizeof(float)=4
sizeof(double)=8
sizeof(long double)=16
(platform dependent)
Limits and literals
#include <iostream>
#include <limits>
int main(){
std::cout << std::numeric_limits<int>::min() << ".." << std::numeric_limits<int>::max() << "\n";
int dec = 42; // decimal
int hex = 0x2A; // hexadecimal
int bin = 0b101010; // C++14 binary literal
auto big = 1'000'000; // digit separators (C++14)
std::cout << dec << "," << hex << "," << bin << "," << big << "\n";
}
Expected Output: first line shows int range; second line: 42,42,42,1000000
Character types
#include <iostream>
#include <string>
int main(){
char c = 'A';
char32_t uni = U'Ω'; // UTF-32 literal
std::cout << c << " " << (int)c << "\n";
std::u32string s(1, uni);
std::cout << s.size() << "\n"; // prints 1
}
Expected Output:
A 65
1
Commonly used keywords (overview)
- Types and storage:
auto, const, constexpr, consteval, constinit, mutable
- Flow:
if, else, switch, case, for, while, do, break, continue, return
- OOP:
class, struct, public, private, protected, virtual, override, final
- Memory:
new, delete
(prefer RAII/smart pointers) - Misc:
namespace, using, template, typename, this, friend, explicit
Fixed-width types and portability
#include <cstdint>
#include <iostream>
int main(){
std::int32_t a = 123; // exactly 32-bit
std::uint64_t b = 456u; // exactly 64-bit unsigned
std::cout << sizeof(a) << "," << sizeof(b) << "\n";
}
Expected Output (example): 4,8
Signed vs unsigned gotchas
#include <iostream>
int main(){
unsigned u = 0;
std::cout << (u - 1) << "\n"; // wrap-around (underflow) on unsigned
}
enum class vs enum
enum class Color { Red, Green, Blue }; // scoped, no implicit to int
// enum { Red, Green, Blue }; // unscoped: names leak into surrounding scope
Modern constants: constexpr, consteval, constinit (overview)
constexpr
: may be evaluated at compile time; usable in constant expressions.consteval
(C++20): must be evaluated at compile time (immediate function).constinit
(C++20): ensures a variable is initialized at compile time (not const).
Common Pitfalls
- Assuming sizes are identical across platforms; use sizeof and numeric_limits instead of hardcoding.
- Confusing
char
as always ASCII; use UTF-aware types/encodings when needed.
Checks for Understanding
- How do you find the maximum value of an
int
? - What does the single quote in numeric literals do?
Show answers
std::numeric_limits<int>::max()
- Digit separators for readability; no change to value.
Exercises
- Print sizes of all fundamental integer and floating-point types on your machine.
- Create variables using decimal, hex, and binary literals that represent the same value and print them.