C++ Variables

What are Variables in C++?

Variables are used to store data in a C++ program. A variable acts like a container that holds information which can be used and modified during program execution.

Each variable has:

• A name
• A data type
• A value

Before using a variable in C++, it must be declared with a suitable data type.

Each variable has:

• A name
• A data type
• A value

Before using a variable in C, it must be declared with a suitable data type.

Example

int age = 20;
float totalMarks = 85.5;
char grade = ‘A’;

Explanation:

  • age is a valid variable name.

  • totalMarks is a valid variable name.

  • grade is a valid variable name.

Rules for Naming Variables

A variable name in C++ must follow certain rules:

• It can contain letters, digits, and underscores (_).
• It cannot start with a digit.
• It cannot contain spaces.
• It cannot use C++ keywords such as int, float, if, while, etc.
• Variable names are case-sensitive.
• Variable names should be meaningful and easy to understand.

Valid Variable Names:

✓ age
✓ student_name
✓ totalMarks
✓ num1

Invalid Variable Names:

✗ 1age
✗ student name
✗ int
✗ total-marks

Variable Declaration in C++

In C++, a variable must be declared before it is used in a program.

The syntax for declaring a variable is:

data_type variable_name
int age;
float price;
char grade;

Explanation

• int age; declares an integer variable.
• float price; declares a floating-point variable.
• char grade; declares a character variable.

A variable declaration tells the compiler the name and data type of the variable.

Variable Initialization in C++

Variable initialization means assigning an initial value to a variable at the time of its declaration.

Syntax

data_type variable_name = value;

Example

int age = 20;
float price = 99.50;
char grade = ‘A’;

Explanation

In this example, three variables are declared and initialized with different data types.

• int age = 20; stores an integer value.
• float price = 99.50; stores a decimal value.
• char grade = 'A'; stores a single character.

These variables hold their assigned values in memory.

Types of Variables in C++

C++ variables can be classified based on where they are declared and how they are used in a program.

Local Variables

A local variable is declared inside a function or a block of code. It can be accessed only within that function or block where it is declared.

Example

#include <iostream>
using namespace std;

int main() {
int age = 20;
cout << age;
return 0;
}

Output

20

Explanation

In this example, the variable age is declared inside the main() function. It stores the value 20 and can be accessed within the main() function.

Global Variables

A global variable is declared outside all functions. It can be accessed by multiple functions within the same program, subject to its scope and visibility.

Example

#include <iostream>
using namespace std;

int age = 25;

int main() {
cout << age;
return 0;
}

Output

25

Explanation

In this example, the variable age is declared outside the main() function. Therefore, it is a global variable and can be accessed inside the main() function.

Static Variables

A static variable is declared using the static keyword. It retains its value between function calls and is initialized only once.

Example

#include <iostream>
using namespace std;

void display() {
static int count = 1;
cout << count << endl;
count++;
}

int main() {
display();
display();
display();
return 0;
}

Output

1

2

3

Explanation

In this example, count is declared as a static variable inside the display() function.

The function is called three times, but the variable retains its previous value between calls. Therefore, the output is 1, 2, and 3.

Reference Variables

A reference variable is an alternative name (alias) for an existing variable. It is declared using the & symbol and must be initialized when it is created.

Example

#include <iostream>
using namespace std;

int main() {
int age = 20;
int &ref = age;

cout << age << endl;
cout << ref;

return 0;
}

Output

20
20

Explanation

In this example, ref is a reference variable that refers to the existing variable age.

Both age and ref refer to the same memory location. Therefore, both display the value 20.

Constants in C++

A constant is a fixed value that cannot be changed during program execution. In C++, the const keyword is used to declare a constant.

Example

#include <iostream>
using namespace std;

int main() {
const int MAX = 100;
cout << MAX;
return 0;
}

Output

100

Explanation

In this example, MAX is declared using the const keyword and assigned the value 100.

The value of MAX cannot be changed after initialization.

Scope of Variables

The scope of a variable defines the region of a program where the variable can be accessed.

Local Scope

A variable declared inside a function or block has local scope. It can be accessed only within that function or block.

Example

#include <iostream>
using namespace std;

int main() {
int number = 10;
cout << number;
return 0;
}

Output

10

Explanation

In this example, the variable number is declared inside the main() function. Therefore, it can be accessed only within the main() function.

Global Scope

A variable declared outside all functions has global scope. It can be accessed by different functions within the program, subject to its visibility.

Example

#include <iostream>
using namespace std;

int number = 50;

int main() {
cout << number;
return 0;
}

Output

50

Explanation

In this example, the variable number is declared outside the main() function. Therefore, it has global scope and can be accessed inside the main() function.

Difference Between Variables and Constants in C++

Variables

Constants

A variable stores a value that can be changed.

A constant stores a fixed value that cannot be changed.

Declared using data types like int, float, and char.

Declared using the const keyword.

Its value can be modified during program execution.

Its value cannot be modified after initialization.

Example: int age = 20;

Example: const int MAX = 100;

Common Mistakes While Using Variables in C++

• Using a variable without declaring it.
• Using an incorrect data type.
• Forgetting to initialize a variable before using its value.
• Using a C++ keyword as a variable name.
• Assigning a value of an incompatible data type.
• Trying to modify a constant variable.
• Accessing a local variable outside its scope.

Example Program

Program

#include <iostream>
using namespace std;

int main() {
int age = 20;
float salary = 25000.50;
char grade = ‘A’;

cout << “Age = ” << age << endl;
cout << “Salary = ” << salary << endl;
cout << “Grade = ” << grade << endl;

return 0;
}

Output

Age = 20
Salary = 25000.5
Grade = A

Explanation

• #include <iostream> → Includes the input-output stream library.
• using namespace std; → Allows the use of standard library names without writing std::.
• int main() → The main function where program execution begins.
• int age = 20; → Declares an integer variable and assigns the value 20.
• float salary = 25000.50; → Declares a floating-point variable.
• char grade = 'A'; → Declares a character variable.
• cout → Displays the variable values on the screen.
• endl → Moves the output to the next line.
• return 0; → Indicates successful program execution.

Conclusion

Variables are an essential part of C++ programming. They help store and manage data during program execution.

Understanding variable declaration, initialization, types, and scope helps in writing clear and efficient C++ programs.

Key Points

• Variables are used to store data in C++ programs.
• Every variable must be declared with a suitable data type.
• Variable names must follow specific naming rules.
• Variables can be initialized at the time of declaration.
• Local variables are declared inside functions or blocks.
• Global variables are declared outside all functions.
• Static variables retain their values between function calls.
• Reference variables act as alternative names for existing variables.
• Constants store fixed values that cannot be changed.
• Variable scope determines where a variable can be accessed.