← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 13 of 15

C++ Variables

Variable Declaration

Declaring a variable reserves a named slot of memory sized for its data type — writing int age; tells the compiler to set aside 4 bytes and refer to that space as age for the rest of its scope. You must declare a variable's type before using it, since C++ needs to know how many bytes to allocate and how to interpret them.

Example: Variable Declaration

cpp
#include <iostream>

int main() {
	int age; // reserves 4 bytes named 'age'
	age = 30;
	std::cout << age << std::endl;
	return 0;
}

Variable Initialization

Initialization sets a variable's starting value in the same statement that declares it, like int age = 25;, which avoids the bug-prone situation of reading an uninitialized variable that holds unpredictable leftover memory. Best practice in C++ is to always initialize a variable at the point of declaration whenever a sensible starting value exists.

Example: Variable Initialization

cpp
#include <iostream>

int main() {
	int age = 25; // declared and initialized in one statement
	std::cout << age << std::endl;
	return 0;
}

Multiple Variables

You can declare several variables of the same type in one line by separating their names with commas, such as int x, y, z;, which keeps related declarations visually grouped. Each name still gets its own independent storage — they just happen to share one type declaration for brevity.

Example: Multiple Variables

cpp
#include <iostream>

int main() {
	int x, y, z;
	x = 1; y = 2; z = 3;
	std::cout << x << " " << y << " " << z << std::endl;
	return 0;
}

Dynamic Initialization

Dynamic initialization means a variable's starting value is computed from an expression at runtime rather than being a fixed literal, for example int area = width * height; where width and height were entered by the user. This is what makes variables genuinely useful beyond constants — their values can depend on other data your program computes as it runs.

Example: Dynamic Initialization

cpp
#include <iostream>

int main() {
	int width = 10;
	int height = 5;
	int area = width * height; // computed at runtime, not a fixed literal
	std::cout << "Area: " << area << std::endl;
	return 0;
}

Variable Scope

A variable's scope determines where in your code it can be accessed: local variables exist only inside the function or block where they're declared and disappear once that block ends, while global variables declared outside any function are visible everywhere in the file. Preferring local variables over global ones keeps functions self-contained and easier to test in isolation.

Example: Variable Scope

cpp
#include <iostream>

int globalCount = 100; // visible everywhere in the file

void localExample() {
	int localCount = 5; // only exists inside this function
	std::cout << localCount << std::endl;
}

int main() {
	localExample();
	std::cout << globalCount << std::endl;
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.