← Back to C Course | Chapter 8: Structures & Unions | Lesson 6 of 7

C के Enumerations

एक enumeration named choices की एक fixed list है, हफ्ते के दिनों या traffic light रंगों की तरह। यह numbers को readable नाम देता है।
Syntax
c
enum EnumName { VALUE_ONE, VALUE_TWO, VALUE_THREE };

enum EnumName variable_name = VALUE_ONE;

Enumeration क्या है?

0, 1, 2 जैसे magic numbers की बजाय status लिखना code को पढ़ना और maintain करना कहीं आसान बनाता है -- if (status == ACTIVE) compare करना तुरंत clear है, if (status == 1) के उलट जिसमें आपको याद रखना होगा 1 का मतलब क्या है।

उदाहरण: What is an Enumeration?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Status { ACTIVE, INACTIVE };
// Program execution starts in main()
int main() {
	enum Status status = ACTIVE;
	// Check whether status == ACTIVE
	if (status == ACTIVE) {
		// Print formatted text to the screen
		printf("Active");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Enum Define करना

यह एक नया type उससे belong करने वाले named integer constants के एक set के साथ declare करता है, कुछ-कुछ वैसे ही जैसे struct एक नया composite type introduce करता है -- दोनों same curly-brace-and-semicolon syntax इस्तेमाल करते हैं।

उदाहरण: Defining an Enum

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
	enum Color c = GREEN;
	// Print formatted text to the screen
	printf("%d", c);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Enum Constant Values

enum Color {RED, GREEN, BLUE} automatically RED=0, GREEN=1, BLUE=2 assign करता है -- यही predictable numbering है जिसकी वजह से enums array indices या switch-case values की तरह seamlessly काम करते हैं।

उदाहरण: Enum Constant Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d %d", RED, GREEN, BLUE);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Custom Enum Values

enum Status {ACTIVE=1, PENDING, CLOSED} explicitly ACTIVE को 1 set करता है, और क्योंकि PENDING और CLOSED unassigned हैं, वे automatically sequence को 2 और 3 की तरह जारी रखते हैं।

उदाहरण: Custom Enum Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Status { ACTIVE = 1, PENDING, CLOSED };
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d %d %d", ACTIVE, PENDING, CLOSED);
	// Return 0 to signal that the program finished successfully
	return 0;
}

switch Statements में Enums इस्तेमाल करना

क्योंकि switch को इसके case labels के लिए constant integer expressions चाहिए, और enum constants exactly वही हैं, ये उन कुछ types में से हैं (plain integers और chars के अलावा) जिन पर switch statements सीधे branch कर सकते हैं।

उदाहरण: Using Enums in switch Statements

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
enum Color { RED, GREEN, BLUE };
// Program execution starts in main()
int main() {
	enum Color c = GREEN;
	// Choose a branch based on the value of c
	switch (c) {
		case RED: printf("Red"); break;
		case GREEN: printf("Green"); break;
		case BLUE: printf("Blue"); break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि compiler आपको एक enum variable को एक invalid integer assign करने से रोकता है, जब C enums को ints जैसे treat करता है।
  2. यह भूल जाना कि values assign न करने पर enumerators default से 0 से शुरू होते हैं।
  3. एक enum को %s से print करना और इसके नाम की उम्मीद करना, जब यह एक integer रखता है और इसे %d चाहिए।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.