Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 3 of 10
Variables, Primitive Types & auto
C++ has a rich type system. Primitive types have platform-dependent sizes (`int` may be 16 or 32 bits). Use `<cstdint>` for guaranteed-width types. The `auto` keyword (C++11) deduces the type from the initialiser - it prefers value types and `sizeof(auto x = ...)` always gives the size of the deduced type, not a reference.
Types, sizeof & auto - All Patterns
#include <iostream>
#include <cstdint> // int8_t, int32_t, uint64_t etc.
#include <limits> // std::numeric_limits
#include <string>
int main() {
// ── Signed integers ──────────────────────────────────────
int a = -42; // at least 16-bit, usually 32-bit
long b = 1'000'000L; // C++14: digit separator '
long long c = 9LL;
short d = 32767;
// ── Guaranteed-width types (always prefer for precise needs) ─
int8_t i8 = 127; // exactly 8-bit signed
uint32_t u32 = 4'294'967'295U; // exactly 32-bit unsigned
int64_t i64 = -1LL; // exactly 64-bit signed
// ── Floating point ───────────────────────────────────────
float f = 3.14f; // 32-bit IEEE 754, suffix f required
double g = 3.141592; // 64-bit (the default)
long double h = 3.14L; // 80-bit on x86
// ── Boolean & char ───────────────────────────────────────
bool flag = true; // sizeof == 1
char ch = 'A'; // signed char (platform-dependent)
char32_t uni = U'U0001F600'; // Unicode code point
// ── auto - type deduction (C++11) ───────────────────────
auto x = 42; // int
auto y = 42.0; // double
auto z = 42.0f; // float
auto s = std::string{"hi"};// std::string (NOT const char*)
auto cs = "hello"; // const char* - NOT std::string!
// auto& and const auto& - preserving references
int val = 10;
auto& ref = val; // int& - modification allowed
ref = 20; // val is now 20
const auto& cref = val; // const int&
// cref = 30; // ← compile error: const
// ── sizeof ───────────────────────────────────────────────
std::cout << "sizeof(int) = " << sizeof(int) << "
"; // 4
std::cout << "sizeof(double) = " << sizeof(double) << "
"; // 8
std::cout << "sizeof(bool) = " << sizeof(bool) << "
"; // 1
std::cout << "sizeof(int64_t)= " << sizeof(int64_t)<< "
"; // 8
// ── Numeric limits ───────────────────────────────────────
std::cout << "INT_MAX = " << std::numeric_limits<int>::max() << "
";
std::cout << "DBL_MAX = " << std::numeric_limits<double>::max() << "
";
// ── Type aliases ─────────────────────────────────────────
using Meter = double; // type alias (C++11, preferred over typedef)
using Index = int;
Meter distance = 100.5;
Index idx = 0;
std::cout << distance << " m, index=" << idx << "
";
return 0;
}Common Mistakes
- `auto cs = "hello"` deduces `const char*`, not `std::string` - if you need a `std::string`, write `auto s = std::string{"hello"}` or `using namespace std::string_literals; auto s = "hello"s;`.
- Integer overflow is undefined behaviour for signed types - `int x = INT_MAX; x++;` is UB in C++. Use `unsigned` or `int64_t` if you need large ranges, and check with `-fsanitize=undefined`.
- `auto` in a range-based for loop copies by default - `for (auto elem : vec)` copies each element. Write `for (const auto& elem : vec)` to avoid copies, or `for (auto& elem : vec)` to modify elements.
Tip
Tip
Practice Variables Primitive Types auto in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Statically-typed data representation in memory with fixed-width specifiers and auto
Practice Task
Note
Practice Task - (1) Write a working example of Variables Primitive Types auto from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Variables Primitive Types auto is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.
Key Takeaways
- C++ has a rich type system.
- `auto cs = "hello"` deduces `const char*`, not `std::string` - if you need a `std::string`, write `auto s = std::string{"hello"}` or `using namespace std::string_literals; auto s = "hello"s;`.
- Integer overflow is undefined behaviour for signed types - `int x = INT_MAX; x++;` is UB in C++. Use `unsigned` or `int64_t` if you need large ranges, and check with `-fsanitize=undefined`.
- `auto` in a range-based for loop copies by default - `for (auto elem : vec)` copies each element. Write `for (const auto& elem : vec)` to avoid copies, or `for (auto& elem : vec)` to modify elements.
Finished this lesson?