top of page

What is Variable in Programming?

In programming, variables are like the building blocks that enable us to create dynamic and interactive applications. Understanding the concept of a variable is an essential step toward mastering any programming language. These are used to store and modify the data, respond to the user input, perform mathematical calculations, and execute algorithms.


In this article, we will understand the concept of variables in programming along with their types. We will guide you through the step-by-step process of using variables in the C++ programming language starting from declaring a variable to using it to achieve the desired results in your code.


Table of content:

STEP 1: Declare a Variable

STEP 2: Assign a Value to a Variable

STEP 3: Use Variable in a Program


What is Variable in Programming?

A variable is a named location in the computer's memory that is used to store data or information that can be used throughout a program. They are also used to control the flow of a program.


Here are some of the benefits of using variables in programming:

  • Variables make programs more readable and maintainable.

  • Variables allow programmers to store data and control the flow of their programs.

  • Variables can be used to store any type of data, including integers, floating-point numbers, strings, and Boolean values.

  • Variables can be used to perform calculations and logical operations.


Types of Variables in Programming

Depending on the programming language and the context, variables can be classified into different types. Here are some common types of variables in programming:


Integer:

  • Stores whole numbers (positive, negative, or zero).

  • Examples: 0, 42, -10, 1000, etc.

  • In some languages, there are different sizes of integers, like int, long, and short.

Floating-Point:

  • Stores decimal numbers or numbers with a fractional part.

  • Examples: 3.14, -0.5, 2.718, etc.

  • In some languages, there are different sizes of floating-point numbers, like float, double.

Character:

  • Stores single characters or small strings with a length of one.

  • Examples: 'A', 'b', '5', '%', etc.

  • In some languages, characters are denoted using single quotes.

String:

  • Stores a sequence of characters or text.

  • Examples: "Hello, World!", "Programming is fun", "12345", etc.

  • In some languages, strings are denoted using double quotes.

Boolean:

  • Stores true or false values.

  • Used in logical operations and conditional statements.

  • Examples: true, false.

Array:

  • Stores a collection of elements of the same data type.

  • Elements are accessed using an index.

  • Examples: An array of integers [1, 2, 3, 4], an array of strings ["apple", "banana", "orange"].

Pointer:

  • Stores memory addresses of other variables.

  • Used for dynamic memory allocation and advanced data structures.

  • Examples: Addresses like 0x7fff5fbff7a8.

Object:

  • Stores data and methods (functions) together as a single entity.

  • It is used in object-oriented programming (OOP) paradigms.

  • Examples: An object of a class representing a car, person, etc.

Enumerated (Enum):

  • Stores a set of named constants that represent integer values.

  • It is used to define a limited set of possible values for a variable.

  • Examples: Days of the week, months of the year.

Constant:

  • A special type of variable whose value cannot be changed after initialization.

  • It is used to define fixed values that remain constant throughout the program's execution.

  • Examples: const PI = 3.14159.


Steps to Use Variables in Programming Languages

To utilize a variable in the C++ programming language, adhere to the steps outlined below:


Variable in programming

STEP 1: Declare a Variable

Variable declaration is the process of introducing a new variable to a programming language. It involves specifying the variable's name and data type to the compiler or interpreter, which reserves memory space for that variable.


The syntax for declaring a variable varies depending on the programming language, but in C++ programming language, it follows the below pattern:

data_type variable_name;

Here, data type represents the type of data the variable can hold. For example, it can be int, double, char, etc. and variable is the name of the variable.


For example, if you want to declare the "age" (variable _name):

   int age;

If you want to declare the "salary" (variable_name):

double salary;

STEP 2: Assign a Value to a Variable

Once a variable is declared, you can assign a value to it using the assignment operator (=). The assigned value must be of the same data type as the variable or a compatible type. The assignment statement typically follows this format:

variable_name = value;

In the previous section, we used two variables, age, and salary. Now, let's assign values to these variables. Take a look at the following code:

int age; // Declaration of the 'age' variable
double salary; // Declaration of the 'salary' variable

// Assigning values to the variables
age = 25;
salary = 50000.75;

STEP 3: Use a Variable in a Program

After declaring and assigning a value to a variable, you can use it throughout your program to perform various operations. You can use variables in mathematical calculations, string manipulation, decision-making (using conditionals), loops, function calls, and more.


Consider the following example we will use the variable to print the age and salary:

#include <iostream>
int main() 
{
    // Declaring and initializing variables
    int age = 25;
    double salary = 50000.75;

    // Using variables in the program
    std::cout << "My age is: " << age << std::endl;
    std::cout << "My salary is: " << salary << std::endl;

    return 0;
}

This will be the output of the above code:

Variable in programming

Scope of a Variable

The scope of a variable refers to the part of the program where the variable is visible and can be accessed. In most programming languages, variables have either global scope or local scope:

  • Global Scope: A variable declared outside any function or block has global scope. It is accessible from any part of the program, including all functions. Global variables typically have a more extended lifetime and can be useful for storing data shared across multiple functions or modules.

  • Local Scope: A variable declared inside a function or block has local scope. It is only accessible within that specific function or block and is not visible outside of it. Local variables have a shorter lifetime and are used to store temporary data relevant to that specific function's execution.

Consider the below code where we are declaring the variable outside the function and also inside the function:

#include <iostream>

int globalVar = 10; // Global variable

void Function() {
    int localVar = 5; // Local variable
    std::cout << globalVar << std::endl; 
    // Accessing the global variable inside the function
    
    std::cout << localVar << std::endl;
}

int main() {
    Function(); 
    // Accessing the local variable inside the function
    
    std::cout << globalVar << std::endl; 
    // Accessing the global variable outside the function
    
    return 0;
}


Lifetime of a Variable

The lifetime of a variable is the period during which the variable exists in memory and retains its value. It is determined by the scope in which the variable is declared.

  • Global variables have a lifetime that extends throughout the program's execution, from the point they are declared until the program terminates.

  • Local variables have a lifetime that starts when the function or block containing them is called or entered and ends when the function or block exits.

Example:

#include <iostream>

void Function() 
{
    int localVar = 5; 
    // local variable is created when the function is called
    
    std::cout << localVar << std::endl;
}

int main() {
    Function(); 
    // local variable is destroyed after the function returns
    
    return 0;
}


Conclusion

In this article, we have explored the concept of variables in programming, discovering how to declare them, assign values, and use them in a powerful and effective manner. By understanding these fundamental aspects, beginners can enhance their coding skills and write efficient, adaptable, and versatile code.

bottom of page