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%

Typedef & Enum: Expressive Code

Lesson 19 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

1. typedef (Giving Types a Nickname)

The typedef keyword allows you to create an alias for an existing data type. It doesn't create a new type; it just gives a more descriptive name to an old one.

typedef unsigned long int ulong;
ulong population = 7000000000;

typedef int score;
score math = 95;

Why use it? It makes your code "self-documenting." It's clearer to see score than just int.

2. enum (The Set of Named Constants)

An enumeration is a user-defined type that consists of a set of named integer constants. By default, the first name is assigned the value 0, the next is 1, and so on.

enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
enum Day today = Wednesday; // today holds the value 2

Benefits of Enums

They make your code much more readable and less prone to errors. if (today == Sunday) is much harder to mess up than if (today == 6).

Custom Values in Enums

You can override the default values:

enum Level { LOW = 10, MEDIUM = 50, HIGH = 100 };

Combining Typedef and Enum

This is the standard way to create clean, boolean-like types in C:

typedef enum { FALSE, TRUE } bool;
bool isFinished = FALSE;
Use Enums for things that have a fixed, limited set of possibilities, like Colors, Game States (Menu, Loading, Playing), or Directions (North, South, East, West).