📦 Variables & Data Types in C
Learn How Computers Store Information
Complete Beginner Guide With Examples 🚀
🚀 Introduction
In the previous lesson, we created our first C program using printf().
But programs need to store information such as numbers, names, marks, and calculations. For this purpose, we use Variables.
"A variable is a named memory location used to store data."
📦 What is a Variable?
A variable is like a container where a computer stores information.
Example:
int age = 25;
Here:
- int → Data type
- age → Variable name
- 25 → Stored value
🧠 How Variables Work in Memory
┌───────────────┐
│ age = 25 │
└───────────────┘
The computer assigns a memory location to every variable.
🔢 Data Types in C Programming
Data types define what type of information a variable can store.
🔢 int
Stores whole numbers.
int marks = 90;
📊 float
Stores decimal numbers.
float price = 99.5;
🔤 char
Stores a single character.
char grade='A';
📝 double
Stores large decimal values.
double pi=3.14159;
💻 Example: Using Variables in C
#include <stdio.h>
int main()
{
int age = 20;
float height = 5.8;
char grade = 'A';
printf("Age = %d", age);
printf("\nHeight = %.1f", height);
printf("\nGrade = %c", grade);
return 0;
}
🖥️ Output
Height = 5.8
Grade = A
📌 Rules for Naming Variables
- Variable name cannot start with a number
- No spaces are allowed
- Use meaningful names
- C keywords cannot be used
- C language is case-sensitive
Correct examples:
totalMarks
accountBalance
🔒 Constants in C
A constant is a value that cannot be changed during program execution.
const int DAYS = 7;
⭐ Why Are Variables Important?
- Store user information
- Perform calculations
- Manage program data
- Create dynamic applications
- Build complex software
🎯 Conclusion
Variables and data types are the building blocks of C programming. Understanding them is essential before learning advanced topics.
No comments:
Post a Comment