Variables, References and Strings · Lesson 4 of 33
Variables, Types, auto and const in C++
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
autotype deductionconst,constexprand 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;
}
true A 92 9876543210 84.75 int: 4 bytes, max 2147483647 long long: 8 bytes, max 9223372036854775807
| Type | Typical size | Use for |
|---|---|---|
bool | 1 byte | true / false |
char | 1 byte | a character |
int | 4 bytes | counts and indexes up to ~2.1 × 10⁹ |
long long | 8 bytes | big values up to ~9.2 × 10¹⁸ (sums, products) |
double | 8 bytes | decimals (~15 significant digits) |
std::size_t | 8 bytes | sizes 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;
}
book: 35 pen: 120 10 99.5 3000000000 Asha 78
auto traps
auto s = "Asha";is aconst char*, not astd::string. Writestd::string s = "Asha";or use"Asha"s.autodrops references and const:auto x = someRef;makes a copy. Writeconst 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;
}
2026 60 144 1180 1234567890123
const | constexpr | |
|---|---|---|
| Meaning | can't be changed after initialisation | value known at compile time (and const) |
| Value from user input? | yes | no |
| Use for | read-only variables and parameters | true 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
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; }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.