Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,365 questions

11,625 answers

573 users

How to use enumeration in C

3 Answers

0 votes
#include <stdio.h>

enum colors { RED, GREEN, BLUE };

void print_color(enum colors color)
{
    switch (color)
    {
       case RED:
         puts("RED");
         break;
       case GREEN:
         puts("GREEN");
         break;    
       case BLUE:
         puts("BLUE");
         break;
       default:
         puts("Color not exist");
         break;        
    }
}

int main(void) 
{
    print_color(GREEN);
    
    enum colors color;
    printf("Enter a number between 0 and 2: ");
    scanf("%d", (int*)&color);
    print_color(color);
  
    return 0;
}

   
/*
run:
 
GREEN
Enter a number between 0 and 2: 2
BLUE

*/

 




answered Aug 29, 2017 by avibootz
0 votes
#include <stdio.h>

enum week { SUN = 1, MON, TUE, WED, THU, FRI, SAT };

int main(void) 
{
    printf("%d\n", SUN);
    printf("%d\n", MON);
    printf("%d\n", TUE);
    printf("%d\n", WED);
    printf("%d\n", THU);
    printf("%d\n", FRI);
    printf("%d\n", SAT);
    
    return 0;
}

   
/*
run:
 
1
2
3
4
5
6
7

*/

 




answered Aug 29, 2017 by avibootz
0 votes
#include <stdio.h>

enum week_days { SUN = 1, MON, TUE, WED, THU, FRI, SAT };

const char* const days[] = { 
                [SUN] = "Sun" , [MON] = "Mon", [TUE] = "Tue", [WED] = "Wed", 
                [THU] = "Thu", [FRI] = "Fri", [SAT] = "Sat", 
            };

int main(void) 
{
    printf("%s\n", days[1]);
    printf("%s\n", days[2]);
    printf("%s\n", days[3]);
    
    return 0;
}

   
/*
run:
 
Sun
Mon
Tue

*/

 




answered Aug 29, 2017 by avibootz
...