Lesson 2 — Beginner

Variables & Data Types

Variable ek aisi jagah hai jahan data save hota hai — jaise dukaan mein har saman ka dabba. Is lesson mein hum dekhenge variable kaise banate hain, kaunsa data type kab use hota hai, aur int/float ke beech ki conversion traps kaise bachein.

⏱ 14 min• Beginner• Introduction

Variable kya hai aur kaise banta hai

WHAT

Variable ek named memory location hai jisme value store hoti hai. C mein variable pehle declare hota hai (type + naam), phir use karta hai. Declaration hi memory decide karti hai — kitni aur kis type ki.

WHEN

Jab bhi kuch save karna ho — marks, naam, price, count — variable chahiye. Program chalte waqt har temporary aur permanent value kisi na kisi variable mein rehti hai.

WHERE

Function ke andar (local), file ke upar (global), ya heap pe (pointer ke through) — variable jagah ke hisaab se alag-alag scope rakhta hai.

int age = 20;          // poora number
float price = 99.5f;    // decimal
char grade = 'A';       // ek character
double pi = 3.14159265; // bada decimal (15-16 digits)

int a, b, c;           // teeno ek saath declare
a = 10; b = 20;        // ab value daali

Core data types aur size

Type      Size*      Format    Example
int       4 bytes   %d        int age = 20;
float     4 bytes   %f        float pi = 3.14f;
double    8 bytes   %f        double big = 1.234567;
char      1 byte    %c        char ch = 'A';
long int  4/8 bytes %ld       long big = 1000000L;
short     2 bytes   %hd       short s = 100;

* typical 32/64-bit compiler pe — hamesha sizeof se check karo
#include <stdio.h>
int main() {
    printf("int=%zu float=%zu char=%zu double=%zu\n",
           sizeof(int), sizeof(float), sizeof(char), sizeof(double));
    return 0;
}

Output (typical):
int=4 float=4 char=1 double=8
Yaad rakho: sizeof operator tumhe actual size batata hai — ratne ki zarurat nahi, compiler pe check karo. Exam mein common answer: int=4, char=1, double=8 bytes.

Qualifiers — const, signed, unsigned

const float PI = 3.14159;   // fix value — badal nahi sakte
// PI = 3.0;                // ERROR: assignment to constant

unsigned int x = 4000000000; // sirf positive — range double
int y = -5;                  // signed by default (negative allowed)

int a = 10;   // variable — badalta rehta hai
const int b = 20; // constant — pura program mein same
Interview trap: Q: "unsigned int mein -1 store hoga?" — technically value wrap ho kar bahut badi positive ban jaayegi (4294967295 on 4 bytes). Range yaad rakho: int = -2^31 se 2^31-1, unsigned int = 0 se 2^32-1.

Type conversion — integer division ka trap

Jab do alag type ke values ka operation hota hai, C chhoti type ko badi type mein convert kar deta hai — ye implicit conversion (automatic promotion) hai. Aur tum khud convert karo toh wo explicit casting.

#include <stdio.h>
int main() {
    int a = 7, b = 2;
    printf("%d\n", a / b);          // 3  — dono int, fraction gaya!
    printf("%f\n", (float)a / b);   // 3.500000 — cast kiya tab sahi

    int x = 5.9;                    // truncation → 5
    float f = 43;                   // promotion  → 43.000000
    printf("%d %f\n", x, f);
    return 0;
}

Output:
3
3.500000
5 43.000000
Sabse bada trap: 7/2 = 3.5 nahi, 3! Jab tak dono operands int hain, fraction kaat diya jaata hai. Decimal chahiye toh kam se kam ek operand ko float banao — (float)a / b.

Exercise

Question: Float value print karne wala format specifier kya hai? (% ke saath)

Question: Char data type ki size kitne bytes hoti hai? Sirf number daalo.

Question: int a = 7, b = 2; a / b ka output kya hai? Sirf number daalo.

Question: Program ke andar value ko badalne se rokne wala keyword kaunsa hai?

Common mistakes

Desi example: Kirane ka bill — float ka dhoka

Dukaan pe chhote decimals ka hisaab aksar "almost" aata hai — yehi float ki asli duniya hai. C mein bhi same: 0.1 + 0.2 exact 0.3 nahi hota:

c
#include <stdio.h>
int main() {
    float a = 0.1, b = 0.2;
    printf("%.10f\n", a + b);
    if (a + b == 0.3) printf("exact 0.3\n");
    else printf("0.3 NAHI hai!\n");
    return 0;
}
Output: 0.3000000119
        0.3 NAHI hai!
Fix: integer paise rakho (20 + 30 = 50 paise) ya tolerance do.
Exam mapping (TCS / GATE / College)
  • TCS NQT: format specifiers (%d, %f, %c, %s) aur sizeof — 1-2Q fixed.
  • GATE: signed/unsigned range aur integer division traps.
  • College exam: type conversion program likhwana — implicit vs explicit.
  • Trap: "int ki size hamesha 4 hai" — compiler/platform pe depend karti hai, sizeof se check karo.
Lesson complete?

Variables aur types clear. Agla lesson — Operators — mein dekhenge calculations, comparisons aur bitwise tricks kaise hote hain.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course