Variables in C

In this class, we will try to understand Variables in C Programming.

For Complete YouTube Video: Click Here

Before understanding the definition of variables, we will understand how we will learn this course.

In this course, we will understand the concepts with respect to memory.

What does that mean?

It means, whenever a line of code is executed, we will visualize the impact created by a line of code on memory.

Visualizing Memory

Let us visualize the memory.

The image below shows how a memory can be visualized.

Memory Visualization

In the above image, the memory is divided into blocks. An address uniquely represents each block.

Variable Definition

Now let us understand the variables. Consider the program we have discussed in our first class.

Introduction to C Programming
Program from first class

The first line of the program is as shown below.

int a, b, c;

In the above line, int represents a datatype.

Int data type means variables a, b, c can store only integers.

a, b, c are the variables in the program.

What is meant by a variable?

Variable is the name of the memory location used to store a value.The value of a variable can be changed from time to time during the execution of the program.

The definition states that a, b, and c represents the names of the memory locations.

Allocation of Memory to Variables

When the line of code is executed, the compile will allocate the memory locations to the variables.

The allocation is random, and it is shown in the image below.

Memory Allocation to Variables

From the image, we can understand that a is allocated to the fourth memory location, b is allocated to the second memory location, and c is allocated to the fifth memory location.

So far, memory locations are allocated.

However, the values are not assigned to those locations.

Assigning Values to the Variables

Let us check the second line of the program.

a = 10, b = 20;

In the second line, the variables a and b are assigned with values 10 and 20.

The image below shows how the impact is created after executing the second line of the program.

Memory after Assigning Values to the Variables
Memory after Assigning Values to the Variables

In the above program, the first two lines can be made into one line by assigning the values to the variable during the declaration itself.

The modified program is as shown below.

int a = 10, b = 20, c;

The declaration and assignment of the variables can be done in one single line.

Changing the values of the variables

The definition also states that the value of a variable can be changed from time to time during the program’s execution.

To understand this point, we will consider the program given below.

Program to Change the Values of the Variables

The first two lines state that a, b, c variables are declared, and a and b are assigned 10 and 20.

In the third line of the program, a is assigned to 30.

Now the value of a which is previously stored with ten is overwritten with 30.

Similarly, after executing the fourth line of the program, the value b will be overwritten with 40.

Changing the Values of the Variables

The image above shows the memory with change in values.