Introduction to Variables

In this tutorial, we will learn about variables and why they are important in programming. We will explore the purpose and benefits of using variables in our programs. We will also discuss whether it is possible to write programs without using variables and if there are any alternative approaches available. So, let’s begin our exploration together and find out more about variables.


What is a variable?

A variable is a name given to a memory location where we can store some data for later use. Think of it as a container that can hold a value, similar to a box full of toys shown below. It has a name so that we can easily refer to it and access the data inside.

A box full of toys

Now that we know what a variable is, let’s explore why we use variables in our programs.


Why do we need variables?

You may question the necessity of variables when we can refer to memory locations using their addresses. Allow me to clarify with a simple question: “How do you access a specific webpage on your browser?” Most likely, you enter the webpage’s URL (e.g., www.google.com) rather than its IP address. Although you can input the cryptic IP address in the browser’s address bar to visit a webpage, there are two significant disadvantages:

  1. You need to remember the IP address of the webpage.
  2. Mistyping a number in the IP address can result in difficulties loading the webpage, with no clear indication of the error.

As humans, we are better at remembering names rather than numbers. Therefore, it is more practical to use human-friendly URLs instead of cryptic IP addresses. This concept extends to memory locations in programming as well. Accessing a memory location by its name is preferable to using a series of numbers. Consequently, there are several reasons why you should utilize variables in your programs:

  1. Variables are human-friendly names assigned to memory locations, providing a convenient means to access and manipulate different memory locations.
  2. Variables enhance the readability of your programs.
  3. Variables save a lot of time updating the different parts of code.

Let me explain the last two points using programming examples.

Let’s consider the following program:

#include <stdio.h>

int main() {
    printf("%d", 35 * 10);
    return 0;
}

The output of the program is 350, which is evident after looking at the printf function. However, can you guess what 35 * 10 is referring to?

Now, let’s consider the following program:

#include <stdio.h>

int main() {
    int length = 35;
    int breadth = 10;

    printf("%d", length * breadth);
    return 0;
}

The output remains the same, but now the code is more readable. The expression length * breadth refers to the area of the rectangle, which is what we are displaying here. So, one thing is clear: variables make your code easier to understand.

Now, let’s understand how variables save a lot of time for programmers when updating different parts of code through an example program.

Suppose we want to calculate the square and cube of the number 10. We can write the following code:

#include <stdio.h>

int main() {
    // calculate the square of a number
    printf("Square of a number: %d\n", 10 * 10);

    // calculate the cube of a number
    printf("Cube of a number: %d", 10 * 10 * 10);
    return 0;
}

Output:

But what if in the future, we decide to calculate the square and cube of the number 20? We would need to make changes to both printf functions as follows:

#include <stdio.h>

int main() {
    // calculate the square of a number
    printf("Square of a number: %d\n", 20 * 20);

    // calculate the cube of a number
    printf("Cube of a number: %d", 20 * 20 * 20);
    return 0;
}

Output:

This may not seem like a big deal, but imagine a program with thousands of lines of code, and you decide to change a specific value that is used in multiple places throughout the code. It can become quite annoying and frustrating. This is where variables come to the rescue.

Now, consider the following program and see for yourself how powerful a variable can be for programmers:

#include <stdio.h>

int main() {
    int n = 10;

    // calculate the square of a number
    printf("Square of a number: %d\n", n * n);

    // calculate the cube of a number
    printf("Cube of a number: %d", n * n * n);
    return 0;
}

By introducing a single variable n in our code, the problem is resolved. Now, if you decide to find the square and cube of 20, you just need to change the value of n to 20, and you’re done.

So, with this, I hope you are motivated to use variables in your code. They can save you a lot of time and make your programming tasks much easier.



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.