C++ Variables

Variables in C++ are used to store data. A variable is a named memory location that can store a value of a specific data type. Here are the key aspects of variables in C++ along with examples:

Variable Declaration

To declare a variable, you specify its data type followed by a name.

int age; // Declares an integer variable named "age" double pi; // Declares a double variable named "pi"

Variable Initialization

You can optionally initialize a variable when declaring it.

int score = 100; // Initializes "score" with the value 100 double temperature = 25.5; // Initializes "temperature" with the value 25.5

Assigning Values

You can change the value of a variable using the assignment operator (=).

age = 30; // Assigns the value 30 to the "age" variable pi = 3.14159; // Assigns the value 3.14159 to the "pi" variable

Variable Scope

The scope of a variable determines where it can be accessed in your program. A variable's scope can be either global or local.

Global Variables

Variables declared outside of any function or block have global scope and can be accessed from anywhere in the program.

int globalVar = 50; // A global variable

Local Variables

Variables declared within a function or block have local scope and are only accessible within that function or block.

void myFunction() { int localVar = 100; // A local variable within the function }

Variable lifetime

The lifetime of a variable determines how long it exists in memory. A variable's lifetime can be either static or dynamic.

  1. Static variables: Static variables are allocated in memory when the program starts and are destroyed when the program ends.
  2. Dynamic variables: Dynamic variables are allocated in memory when they are needed and are destroyed when they are no longer needed.
// Global variable int global_variable = 10; // Local variable void my_function() { int local_variable = 20; // Access global variable std::cout << global_variable << std::endl; // Access local variable std::cout << local_variable << std::endl; } int main() { // Access global variable std::cout << global_variable << std::endl; // Call my_function() my_function(); return 0; }


Constants are variables whose values cannot be changed after initialization.

const int maxScore = 100; // Declares a constant "maxScore" with an initial value of 100

Naming Rules

Variable names are case-sensitive and must start with a letter or underscore. They can contain letters, digits, and underscores.

int _count = 5; // Variable name starts with an underscore double temperature_2 = 25.5; // Variable name contains an underscore


Variables are named storage locations used to store and manage data. They are declared with a specific data type, can be initialized with values, have a scope that defines where they can be accessed, and can be either global (accessible throughout the program) or local (restricted to a specific function or block). Understanding how to declare and use variables is essential for data manipulation in C++ programs.