AnyCompiler

Enums aur typedef

enum Day { MONDAY, TUESDAY, WEDNESDAY } teen named constants declare karta hai jo underneath actually sirf ints hote hain, automatically 0 se number hote hue — MONDAY 0 hai, TUESDAY 1 hai, WEDNESDAY 2 hai. Ye #define constants ki lambi list ka C ka lightweight alternative hai.

typedef

typedef ek existing type ko ek naya naam deta hai. Ye structs ke saath sabse useful hai: iske bina, har declaration ko struct keyword chahiye (struct Point p;) — typedef struct { ... } Point; se aap bas Point p; likh sakte ho, jo zyadatar real C codebases ka convention hai.

#include <stdio.h>

enum Day { MONDAY, TUESDAY, WEDNESDAY };

typedef struct {
    int x;
    int y;
} Point;

int main() {
    enum Day today = TUESDAY;
    printf("%d\n", today);
    Point p = {1, 2};
    printf("%d %d\n", p.x, p.y);
    return 0;
}
Output
1
1 2

Aage: const aur static — kisi cheez ko unmodifiable mark karna, aur phir bhi try karne par aane wala real compiler error.

Practice exercise

enum Day { MONDAY, TUESDAY, WEDNESDAY } define karo. Ek variable today ko WEDNESDAY set karo aur use int ke roop mein print karo.