Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 3 — Enums and Typedef

Lesson 16 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Enums and Typedef in C

Enumeration (enum)

An enum is a user-defined data type consisting of named integer constants. It improves code readability.

Syntax:

enum enum_name { constant1, constant2, constant3, ... };

Example:

enum Day { MON, TUE, WED, THU, FRI, SAT, SUN };

enum Day today = WED;
printf("Day number: %d\n", today);  /* prints 2 (starts from 0) */

Custom values:

enum Status {
    PENDING = 1,
    ACTIVE = 2,
    INACTIVE = 3
};

Enum in switch:

switch (today) {
    case MON: printf("Monday"); break;
    case FRI: printf("Friday"); break;
    default:  printf("Other day");
}

---

Enum vs #define

Featureenum#define
Type safetyYes (compiler checks)No
DebuggableYes (names visible in debugger)No
ScopeFollows C scope rulesGlobal
ArithmeticAllowedAllowed

---

typedef

typedef creates an alias for an existing data type. It does not create a new type — just a new name.

Syntax:

typedef existing_type new_name;

Examples:

typedef int Integer;
typedef float Real;
typedef char* String;

Integer x = 10;
Real pi = 3.14;

typedef with struct:

/* Without typedef: */
struct Point { int x, y; };
struct Point p1;

/* With typedef: */
typedef struct {
    int x, y;
} Point;

Point p1;   /* no need to write 'struct' */

typedef with enum:

typedef enum { RED, GREEN, BLUE } Color;

Color c = GREEN;

typedef with pointer:

typedef int* IntPtr;
IntPtr p;   /* same as: int *p; */

---

Combined Example

#include <stdio.h>

typedef enum { CIRCLE, RECTANGLE, TRIANGLE } ShapeType;

typedef struct {
    ShapeType type;
    float area;
} Shape;

int main() {
    Shape s;
    s.type = CIRCLE;
    s.area = 3.14 * 5 * 5;
    printf("Area = %.2f\n", s.area);
    return 0;
}