Variables in C++
In C++, a variable is a named storage location in memory that can hold a value. Unlike constants, the value of a variable can be changed during the execution of a program.
A variable acts as a container to store data values such as numbers, characters, or
strings.
For example, int age = 20; creates a variable named
age.
Syntax:
data_type variable_name = value;
Why Use Variables?
- To store and manage data during program execution.
- To make programs flexible and reusable.
- To perform operations on stored data (e.g., calculations, comparisons).
Rules for Naming Variables
- Variable names must begin with a letter (a–z, A–Z) or underscore (_).
- Subsequent characters can include letters, digits, or underscores.
- No spaces or special characters are allowed.
- C++ is case-sensitive →
Ageandageare different. - Variable names should be meaningful for better readability.
Understanding these rules in more detail:
While declaring variables in C++, we must follow these rules:
-
Variable names must begin with a letter (a–z, A–Z) or underscore (_)
Valid example:
_valid = 100;
name = "Angel";
Invalid example:
2name = 50; // Invalid: starts with a number @age = 20; // Invalid: starts with special character -
Variable names can only contain letters, digits, and underscores
Valid example:
marks_1 = 95;
roll_number = 25;
Invalid example:
my-var = 20; // Invalid: contains hyphen my var = 100; // Invalid: contains space -
C++ is case-sensitive
This means
Ageandageare different variables.Example:
int Age = 18; int age = 20; cout << "Age = " << Age << endl; cout << "age = " << age << endl;Output:
Age = 18
age = 20
-
Variable names should not be C++ reserved keywords
Examples of reserved words:
int,class,return,forExample:
int = 100; // Invalid: 'int' is a keyword class = 5; // Invalid: 'class' is a keyword -
Variable names should be meaningful
Prefer
total_marksinstead oftmfor better readability.
Example Program: Simple Variable Usage
// Simple program to demonstrate variables in C++
#include <iostream>
using namespace std;
int main() {
int number = 10; // Variable declaration and initialization
cout << "Initial value of number: " << number << endl;
// Changing the value of variable
number = 25;
cout << "Value of number after change: " << number << endl;
return 0;
}
Output:
Initial value of number: 10
Value of number after change: 25
Explanation:
int number = 10;→ A variable named number is declared and its initial value is 10.coutis used to display the value of the variable on the screen.number = 25;→ The old value of number is replaced with 25. This shows that variable values can be changed any time.endlis used aftercoutso that the next output comes in a new line.- In short: A variable is like a container in memory. We can store a value in it and change that value whenever needed.
Real-Life Analogy:
Think of a variable as a box with a label on it. You can put something inside the box, take it out, or replace it with something else. Similarly, in C++, variables are containers that can hold and change values during program execution.
Common Mistakes with Variables
- Using a variable without initializing it first.
- Declaring variables with confusing names (like
x1, x2instead ofmarks, total). - Forgetting that C++ is case-sensitive (e.g.,
Num≠num).
Summary:
- Variables are containers for storing data values that can change during program execution.
- C++ variables are typed, meaning their type (int, float, string, etc.) must be specified.
- Variable names follow specific rules and cannot use reserved keywords.
- Best practice: use clear, meaningful names and initialize variables properly.