AnyCompiler

Variables aur Types

C ke primitive types mein int, double, aur char shamil hain — har variable ka ek declared type hota hai, aur declare hone ke baad wo type fix ho jata hai. printf ko har type ke liye matching format specifier chahiye: int ke liye %d, 2 decimal places wale double ke liye %.2f, ek single char ke liye %c.

#include <stdio.h>

int main() {
    int age = 30;
    double price = 19.99;
    char grade = 'A';
    printf("%d %.2f %c\n", age, price, grade);
    return 0;
}
Output
30 19.99 A

Har type waqai kitna bada hai?

Un languages ke bar'aks jin mein garbage collector memory layout ko aap se chhupata hai, C mein sizeof se type sizes directly inspect kiye ja sakte hain — aur ye matter karta hai, kyunke ye decide karta hai ke ek variable memory mein exactly kitni jagah leta hai.

#include <stdio.h>

int main() {
    printf("sizeof(int) = %zu bytes\n", sizeof(int));
    printf("sizeof(double) = %zu bytes\n", sizeof(double));
    printf("sizeof(char) = %zu bytes\n", sizeof(char));
    return 0;
}
Output
sizeof(int) = 4 bytes
sizeof(double) = 8 bytes
sizeof(char) = 1 bytes

Aage: operators aur control flow — if/else, for, while, aur switch.

Practice exercise

Ek variable temperature, type double ka, 72.5 par set karein aur ek variable isSunny, type int ka, 1 par set karein (C mein stdbool.h se pehle koi built-in bool nahi hai). Dono ko ek space se separate kar ke sath mein print karein.