C के Enumerations
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक Enum Define करना
यह एक नया type उससे belong करने वाले named integer constants के एक set के साथ declare करता है, कुछ-कुछ वैसे ही जैसे struct एक नया composite type introduce करता है -- दोनों same curly-brace-and-semicolon syntax इस्तेमाल करते हैं।
उदाहरण: Defining an Enum
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Custom Enum Values
enum Status {ACTIVE=1, PENDING, CLOSED} explicitly ACTIVE को 1 set करता है, और क्योंकि PENDING और CLOSED unassigned हैं, वे automatically sequence को 2 और 3 की तरह जारी रखते हैं।
उदाहरण: Custom Enum Values
// 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;
}
Login to try C/C++/Java/PHP code in the editor
switch Statements में Enums इस्तेमाल करना
क्योंकि switch को इसके case labels के लिए constant integer expressions चाहिए, और enum constants exactly वही हैं, ये उन कुछ types में से हैं (plain integers और chars के अलावा) जिन पर switch statements सीधे branch कर सकते हैं।
उदाहरण: Using Enums in switch Statements
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि compiler आपको एक enum variable को एक invalid integer assign करने से रोकता है, जब C enums को ints जैसे treat करता है।
- यह भूल जाना कि values assign न करने पर enumerators default से 0 से शुरू होते हैं।
- एक enum को
%sसे print करना और इसके नाम की उम्मीद करना, जब यह एक integer रखता है और इसे%dचाहिए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: