#include <stdio.h>
/*
Enumeration or Enum in C is a special kind of data type defined by the user.
It consists of constant integrals or integers that are given names by a user.
The use of enum in C to name the integer values makes the entire program easy
to learn, understand, and maintain by the same or even different programmer.
Syntax to Define Enum in C
An enum is defined by using the ‘enum’ keyword in C, and the use of a comma
separates the constants within. The basic syntax of defining an enum is:
enum enum_name{int_const1, int_const2, int_const3, …. int_constN};
In the above syntax, the default value of int_const1 is 0, int_const2 is 1, int_const3 is 2, and so on. However, you can also change these default values while declaring the enum. Below is an example of an enum named cars and how you can change the default values.
enum cars{BMW, Ferrari, Jeep, Mercedes-Benz};
Here, the default values for the constants are:
BMW=0, Ferrari=1, Jeep=2, and Mercedes-Benz=3. However, to change the default values, you can define the enum as follows:
enum cars{
BMW=3,
Ferrari=5,
Jeep=0,
Mercedes-Benz=1
};
*/
typedef enum{
one,
two,
three,
four,
}Counter;
int enum_switch_test(Counter count){
switch(count){//remember ouside of case se cannot add any print statements code crashes
case one:
printf("one");
break;
case two:
printf("two");
break;
case three:
printf("three");
break;
case four:
printf("four");
break;
}
}
int main(int argc,char *argv[]){
Counter count = two;
enum_switch_test(count);
return 0;
}
#if 0
#include <stdio.h>
enum days{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
int main(){
// printing the values of weekdays
for(int i=Sunday;i<=Saturday;i++){
printf("%d, ",i);
}
return 0;
}
int main() {
char option = 'b';
switch (option)
{
case 'a':
printf("Case a hit.");
break;
case 'b':
printf("Case b hit. ");
break;
case 'c':
printf("Case c hit. ");
break;
default:
printf("Default case hit.");
}
}
int main() {
int var = 10;
switch (var)
{
case 5:
printf("Case 1 executed.");
// break;
case 10:
printf("Case 2 executed. ");
// break;
case 15:
printf("Case 3 executed. ");
// break;
case 20:
printf("Case 4 executed. ");
// break;
default:
printf("Default case executed. ");
}
}
int main() {
char c;
c=getchar();// we can only use this one
switch (c){
case 'd': printf("no break stm\n"); //no break so continue
case 'a': printf("a is the char\n");
break;
case 'b': printf("b is the char\n");
break;
default: printf("invalid char\n");
}
printf("Hello world!\n");
return 0;
}
#endif
To embed this program on your website, copy the following code and paste it into your website's HTML: