C++ — Variables
Declaration and initialization
C++ offers multiple ways to initialize variables:
#include <iostream>
using namespace std;
int main() {
int a = 10; // Copy initialization
int b(20); // Direct initialization
int c{30}; // Brace initialization (C++11, preferred)
int d = {40}; // Copy-list initialization
cout << a << " " << b << " " << c << " " << d << endl;
return 0;
}
auto keyword
Let the compiler deduce the type:
#include <iostream>
using namespace std;
int main() {
auto x = 42; // int
auto y = 3.14; // double
auto z = "Hello"; // const char*
auto flag = true; // bool
cout << typeid(x).name() << endl; // i (int)
cout << typeid(y).name() << endl; // d (double)
return 0;
}
Reference variables
Create aliases for existing variables:
#include <iostream>
using namespace std;
int main() {
int x = 10;
int &ref = x; // ref is an alias for x
ref = 20;
cout << x << endl; // 20 — x changed!
const int &cref = x; // Const reference — can't modify
// cref = 30; // Error
cout << cref << endl;
return 0;
}
Scope and lifetime
#include <iostream>
using namespace std;
int globalVar = 100; // Global scope
int main() {
int localVar = 50; // Local to main
{
int blockVar = 25; // Local to this block
cout << "Block: " << blockVar << endl;
}
// cout << blockVar; // Error: out of scope
static int count = 0; // Static: persists between calls
count++;
cout << "Count: " << count << endl;
return 0;
}
Constants
#include <iostream>
using namespace std;
int main() {
const int MAX = 100; // Compile-time constant
constexpr int SIZE = 10; // Guaranteed compile-time
// MAX = 200; // Error: cannot modify const
// #define (old style, avoid)
#define PI 3.14159
cout << "MAX: " << MAX << endl;
cout << "PI: " << PI << endl;
return 0;
}
Type deduction with decltype
#include <iostream>
using namespace std;
int main() {
int x = 42;
decltype(x) y = 100; // y is int
auto add = [](int a, int b) { return a + b; };
decltype(add) lambda2 = add; // Same type as add
cout << y << endl;
return 0;
}
Type sizes
#include <iostream>
using namespace std;
int main() {
cout << "char: " << sizeof(char) << " bytes" << endl;
cout << "short: " << sizeof(short) << " bytes" << endl;
cout << "int: " << sizeof(int) << " bytes" << endl;
cout << "long: " << sizeof(long) << " bytes" << endl;
cout << "long long:" << sizeof(long long) << " bytes" << endl;
cout << "float: " << sizeof(float) << " bytes" << endl;
cout << "double: " << sizeof(double) << " bytes" << endl;
return 0;
}
Scoped enums (enum class)
#include <iostream>
using namespace std;
enum class Color { RED, GREEN, BLUE };
enum class Direction { UP, DOWN, LEFT, RIGHT };
int main() {
Color c = Color::RED;
Direction d = Direction::UP;
// Must cast to int for output
cout << "Color: " << static_cast<int>(c) << endl;
cout << "Direction: " << static_cast<int>(d) << endl;
return 0;
}
Best practices
- Use
autowhen the type is obvious from context - Prefer
constexprover#definefor constants - Use
constfor values that shouldn't change - Prefer brace initialization
{}to prevent narrowing - Use
enum classover plainenumfor type safety
Mini Practice
Write C++ code that:
- Declares variables using all three initialization styles
- Uses
autoto deduce types for different values - Creates a reference variable and shows it modifies the original
- Demonstrates
constandconstexpr
Up Next
In the next lesson, you'll learn about Data Types — the full set of types in C++.
Related Topics
Frequently Asked Questions about Variables
What is Variables in C++?
Variables is a fundamental concept in C++. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Variables?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Variables.
Why is Variables important in C++?
Variables is essential for C++ development. Understanding this concept will help you write better code and solve real-world problems more effectively.