Enumerated Data Type
Posted by Circuit Negma on September 22, 2008
Create By: Hussein Nosair
1: /***********************************************************************************
2: * *
3: * Created By : Hussein Nosair *
4: * Date : 9/22/2008 *
5: * File : enumTest.c *
6: * Description : *
7: * This code tests the enum variables and *
8: * declarations. *
9: * Definition : *
10: * 1. enum (enumerated data type): is a user created data type *
11: * with its own specified values in a user defined list *
12: * ex: *
13: * enum Flags {true, false}; *
14: * I have created a data type called Flags and it has only 2 *
15: * values. You could consider Flags as a predefined data types *
16: * like int, char, double, and long. *
17: * *
18: * enum Flags tst; *
19: * tst is a variable of Flags data type with valid values of true *
20: * and false only and nothing else. *
21: * *
22: * tst = true; VALID *
23: * tst = false; VALID *
24: * tst = gdfgkieroi; NOT VALID *
25: * tst = 5345234256; NOT VALID *
26: * Reference: *
27: * "A First Book of C: Fundamentals of C Programming" Gary Bronson& Stephen Menconi*
28: ***********************************************************************************/
29:
30: // define the required library for this project
31: #include <stdio.h> // Input/Output Library
32:
33: // define user functions and routines
34: void Generic1(void)
35: {
36: // The following variable is only local to Generic1() function, and it is
37: // not accessible by any functions out side Generic1() function.
38: static enum _Color
39: {
40: red, // Index = 0
41: green, // Index = 1
42: yellow // Index = 2
43: } iColor = red;
44:
45: switch(iColor)
46: {
47: case red:
48: printf(" This is =RED= state\n");
49: iColor = yellow;
50: break;
51: case green:
52: printf(" This is =GREEN= state\n");
53: break;
54: case yellow:
55: printf(" This is =YELLOW= state\n");
56: iColor = 1;
57: break;
58: default:
59: printf(" This is =default= state\n");
60: break;
61: }
62:
63: }
64:
65: void Generic2(void)
66: {
67: // The following variable is only local to Generic1() function, and it is
68: // not accessible by any functions out side Generic1() function.
69: enum time
70: {
71: am,
72: pm
73: }iTime = am;
74:
75: switch(iTime)
76: {
77: case am:
78: printf(" This is =AM= state\n");
79: iTime = pm;
80: break;
81: case pm:
82: printf(" This is =PM= state\n");
83: break;
84: default:
85: printf(" This is =default= state\n");
86: break;
87: }
88:
89: }
90:
91: // Main Function
92: int main(void)
93: {
94: printf("/**************************************\n");
95: printf(" * Created By : Hussein Nosair *\n");
96: printf(" * Date : 9/22/2008 *\n");
97: printf(" * File : enumTest.c *\n");
98: printf(" **************************************\n");
99:
100: printf("\n");
101: printf("***************************************\n");
102: printf("* Press 'ENTER' to see result *\n");
103: printf("* Press 'TAB' then 'ENTER' to Exit *\n");
104: printf("***************************************\n");
105:
106: printf("\n");
107: printf("****--> Pay close attention to the state of each function\n");
108: printf(" for every time you hit enter\n");
109: printf("\n");
110:
111: while((getchar() != '\t'))
112: {
113: printf("---***** Generic1 Function *****---\n");
114: Generic1();
115: printf("\n");
116: printf("---***** Generic2 Function *****---\n");
117: Generic2();
118: printf("*----------------------------------------*\n");
119: }
120:
121: printf("\n");
122: printf("***************************************\n");
123: printf("* Press 'ENTER' to exit *\n");
124: printf("***************************************\n");
125: getchar();
126:
127: return 0;
128: }
Result:
