๐๏ธ Introduction
Hello everyone!
Welcome to this important chapter โ What is a Variable?
In the previous lessons, we learned about data and data types. We understood that data represents information and data types...Read More
๐ Where is this data stored inside a program?
The answer is โ Variables.
In this chapter, we will understand clearly and step by step what a variable is, why it is needed, how it works, and why it is one of the most important concepts in programming...Read More
๐น Step 1: Basic Meaning of a Variable
A variable is a name given to a memory location.
In simple words:
๐ A variable is a container used to store data.
Just like in real life we use containers to store things like water, sugar, or rice, in programming we use variables to store...Read More
๐น Step 2: Why Do We Need Variables?
Programs are dynamic. That means values can change during execution.
For example:
A studentโs marks may change.
A bank balance may increase or decrease.
A counter in a loop keeps changing.
If we do not use variables, we cannot store or update...Read More
๐น Step 3: Variable and Memory Relationship
When we create a variable, the computer allocates memory for it.
Each variable:
Has a name
Has a data type
Stores a value
Occupies memory space
For example, if we declare an integer variable, the system reserves memory (usually 4 bytes) to store that integer value...Read More
๐น Step 4: Declaring a Variable
Before using a variable in C, we must declare it.
Declaration means telling the compiler:
The data type of the variable
The name of the variable
For example:
If we want to store age:
We declare an integer variable...Read More
๐น Step 5: Initializing a Variable
After declaring a variable, we can assign it a value.
This is called initialization.
For example:
We declare a variable and assign a value like 20.
The value stored inside a variable can change during...Read More
๐น Step 6: Rules for Naming Variables
C language has specific rules for naming variables.
A variable name:
Must begin with a letter or underscore.
Cannot begin with a number.
Cannot contain special symbols like @, #, or %.
Cannot be a reserved keyword like int, float, or...Read More
๐น Step 7: Types of Variables Based on Scope
Variables can also be classified based on where they are declared.
Local Variables
Declared inside a function.
They can only be used within that function.
Global Variables
Declared outside all functions.
They can be accessed throughout the program...Read More
๐น Step 8: Lifetime of a Variable
Variables also have a lifetime.
Local variables:
Created when a function starts.
Destroyed when the function ends.
Global variables:
Created when the program starts.
Destroyed when the program ends...Read More
๐น Step 9: Real-Life Example
Letโs imagine a simple student program.
We need to store:
Roll number
Name
Marks
Each of these is stored in a variable.
If marks change, we update the variable...Read More
๐น Step 10: Importance of Variables in Programming
Variables are important because:
They store input data.
They hold intermediate results.
They help in calculations.
They control program flow.
They store output results.
Every program you write will use variables...Read More
๐ Summary
In this chapter, we learned:
A variable is a named memory location.
It is used to store data.
Its value can change during execution.
It must be declared before use.
It follows specific naming rules...Read More
Top comments (0)