0% found this document useful (0 votes)
11 views2 pages

C Interview Enums

Enums in C are used to assign names to integer constants, enhancing code readability and grouping related values. They help avoid magic numbers, provide compiler type checking, and facilitate easier maintenance and custom value assignments. A real-life example includes controlling a traffic light system using enums for different light states.

Uploaded by

Yogesh Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

C Interview Enums

Enums in C are used to assign names to integer constants, enhancing code readability and grouping related values. They help avoid magic numbers, provide compiler type checking, and facilitate easier maintenance and custom value assignments. A real-life example includes controlling a traffic light system using enums for different light states.

Uploaded by

Yogesh Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

C Language Interview Preparation

Enums in C
Enums (short for enumerations) are used in C to assign names to a set of integer constants.
They improve code readability, avoid magic numbers, and help group related values
together.

Uses of Enums
1. Improved Readability:

enum State { IDLE, RUNNING, STOPPED };

2. Group Related Constants Together:

enum Direction { NORTH, EAST, SOUTH, WEST };

3. Avoid Magic Numbers:

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

4. Compiler Type Checking:

void setMode(enum Mode m);

5. Easier Maintenance:

If you change an enum value, it updates everywhere.

6. Custom Value Assignments:

enum ErrorCode { OK = 0, ERROR = 100, TIMEOUT = 200 };

7. Switch-Case Efficiency:

switch (direction) {
case NORTH: moveUp(); break;
case SOUTH: moveDown(); break;
}
Real-Life Example

enum TrafficLight { RED, YELLOW, GREEN };

void controlTraffic(enum TrafficLight light) {


switch (light) {
case RED: stop(); break;
case GREEN: go(); break;
case YELLOW: slowDown(); break;
}
}

You might also like