LearnFrenzy
Sign in Take a free test

Variables, References and Strings · Lesson 4 of 33

Variables, Types, auto and const in C++

5 min read Updated Nov 7, 2026 Saurabh Samir

C++ shares C's basic types – int, double, char – and adds bool, safer ways to initialise variables, type deduction with auto, and compile-time constants with constexpr. These small features make everyday code shorter and catch real bugs at compile time.

In this lesson

  • Fundamental types and their sizes
  • Brace initialisation and narrowing
  • auto type deduction
  • const, constexpr and fixed-width integers

Fundamental types

#include <iostream>
#include <limits>

int main() {
    bool passed = true;
    char grade = 'A';
    int marks = 92;
    long long views = 9'876'543'210LL;      // ' separators make big numbers readable (C++14)
    double average = 84.75;

    std::cout << std::boolalpha << passed << ' ' << grade << ' ' << marks << ' '
              << views << ' ' << average << '\n';
    std::cout << "int: " << sizeof(int) << " bytes, max " << std::numeric_limits<int>::max() << '\n';
    std::cout << "long long: " << sizeof(long long) << " bytes, max "
              << std::numeric_limits<long long>::max() << '\n';
    return 0;
}

Output

true A 92 9876543210 84.75
int: 4 bytes, max 2147483647
long long: 8 bytes, max 9223372036854775807
TypeTypical sizeUse for
bool1 bytetrue / false
char1 bytea character
int4 bytescounts and indexes up to ~2.1 × 10⁹
long long8 bytesbig values up to ~9.2 × 10¹⁸ (sums, products)
double8 bytesdecimals (~15 significant digits)
std::size_t8 bytessizes and indexes returned by the STL (unsigned)

Brace initialisation stops narrowing

#include <iostream>

int main() {
    int a = 7.9;          // allowed (with a warning): silently becomes 7
    int b(7.9);           // same
    // int c{7.9};        // ERROR: narrowing conversion from double to int
    int d{7};             // fine
    int e{};              // value-initialised: 0
    double f{};           // 0.0

    std::cout << a << ' ' << b << ' ' << d << ' ' << e << ' ' << f << '\n';
    return 0;
}

Braces {} refuse conversions that lose information, so a whole class of silent bugs becomes a compile error. Empty braces give a guaranteed zero – a good habit for every local variable.

Which form to use

Prefer int x{5}; or auto x = 5;. The older int x = 7.9; compiles with only a warning, which is exactly how precision bugs slip into real code.

auto: let the compiler deduce the type

#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
    auto count = 10;            // int
    auto price = 99.5;          // double
    auto big = 3'000'000'000LL; // long long
    auto name = std::string{"Asha"};

    std::vector<int> marks{78, 91, 64};
    auto it = marks.begin();    // instead of std::vector<int>::iterator

    std::map<std::string, int> stock{{"pen", 120}, {"book", 35}};
    for (const auto& [item, qty] : stock) {          // structured bindings (C++17)
        std::cout << item << ": " << qty << '\n';
    }
    std::cout << count << ' ' << price << ' ' << big << ' ' << name << ' ' << *it << '\n';
    return 0;
}

Output

book: 35
pen: 120
10 99.5 3000000000 Asha 78

auto traps

  • auto s = "Asha"; is a const char*, not a std::string. Write std::string s = "Asha"; or use "Asha"s.
  • auto drops references and const: auto x = someRef; makes a copy. Write const auto& to avoid copies in loops.
  • Use auto when the type is obvious or long (iterators); write the type when it helps readers.

const and constexpr

#include <cstdint>
#include <iostream>

constexpr int MAX_STUDENTS = 60;                // known at compile time
constexpr double GST_RATE = 0.18;
constexpr int square(int n) { return n * n; }   // can run at compile time

int main() {
    const int year = 2026;                      // read-only after initialisation
    int marks[MAX_STUDENTS]{};                  // constexpr works as an array size
    constexpr int area = square(12);            // computed by the compiler

    std::int64_t paise = 1'234'567'890'123;     // exactly 64 bits on every platform
    std::cout << year << ' ' << sizeof(marks) / sizeof(marks[0]) << ' ' << area << ' '
              << 1000 * (1 + GST_RATE) << ' ' << paise << '\n';
    return 0;
}

Output

2026 60 144 1180 1234567890123
constconstexpr
Meaningcan't be changed after initialisationvalue known at compile time (and const)
Value from user input?yesno
Use forread-only variables and parameterstrue constants, array sizes, compile-time functions

Prefer constexpr to #define for constants: it has a type, respects scope and shows up in the debugger.

Practice

Try it yourself

  1. Use constexpr to define the speed of light (299,792,458 m/s) and print how far light travels in 8 minutes 20 seconds, as a long long.

    Show solution
    #include <iostream>
    
    int main() {
        constexpr long long LIGHT_SPEED = 299'792'458;   // m/s
        constexpr long long seconds = 8 * 60 + 20;
        std::cout << LIGHT_SPEED * seconds << " metres\n";
        return 0;
    }

    Output

    149896229000 metres

Quick check

What does int x{3.5}; do?

It's a compile error – brace initialisation forbids narrowing.

What type is auto v = 10LL;?

long long.

const vs constexpr?

const = read-only; constexpr = also known at compile time.

Summary

  • Use int for counts, long long for big values, double for decimals, bool for flags.
  • Brace initialisation forbids narrowing; empty braces give zero.
  • auto deduces types (watch string literals and references); constexpr makes true constants.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links