Variables & Constants in C

 

💻 Variables & Constants in C (Easy English Explanation)

This is a very important basic concept in C programming 📚
If you understand this, coding becomes much easier 🚀


🔹 1. Variables in C 🏷️

A variable is a container used to store data.
👉 Its value can change during program execution.


✅ Example

int a = 10;

👉 Here:

  • int → data type
  • a → variable name
  • 10 → value

🔹 Real-Life Example 🤔

Think of a variable like a box 📦

  • You can put something inside
  • You can change it anytime

🔹 Types of Variables

1. Integer Variable

int age = 20;

2. Float Variable

float price = 99.5;

3. Character Variable

char grade = 'A';

🔹 Rules for Variables ⚠️

✔ Must start with a letter or _
✔ Cannot contain spaces
✔ Cannot be a keyword
✔ Case-sensitive (a and A are different)



🔹 2. Constants in C 🔒

A constant is a value that cannot be changed once assigned.


✅ Example

const int x = 10;

👉 Now, the value of x cannot be changed


🔹 Types of Constants

1. Integer Constant

10, -5, 100

2. Floating Constant

3.14, 0.5

3. Character Constant

'A', 'B'

4. String Constant

"Hello"

🔹 Real-Life Example 🤔

Think of a constant like a fixed rule 📜

  • Once set, it cannot change

🔹 Difference: Variables vs Constants 🆚

FeatureVariables 🏷️Constants 🔒
ValueCan changeCannot change
UseFor changing dataFor fixed data
Exampleint a = 10;const int a = 10;

🔹 Example Program 🧠

#include <stdio.h>

int main() {
int a = 10; // variable
const int b = 20; // constant

a = 15; // allowed
// b = 25; ❌ not allowed

printf("%d %d", a, b);
return 0;
}

🔹 Final Concept 🔥

👉 Variable = value can change
👉 Constant = value is fixed


📝 Summary

  • Variables store data that can change 📦
  • Constants store fixed data 🔒
  • Both are used to manage data in programs

Comments

Popular posts from this blog

Introduction to Computer

History of Computer

Computer Generation