Skip to content

Instantly share code, notes, and snippets.

@elowy01
Last active October 4, 2020 08:38
Show Gist options
  • Save elowy01/fa6b8288acea9a3621f6a94fe9b62ea3 to your computer and use it in GitHub Desktop.
Save elowy01/fa6b8288acea9a3621f6a94fe9b62ea3 to your computer and use it in GitHub Desktop.
## ENUMS
// An example program to demonstrate working
// of enum in C
#include<stdio.h>
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};
int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
}
## it will print out 2
//
// Another example program to demonstrate working
// of enum in C
#include<stdio.h>
enum year{Jan, Feb, Mar, Apr, May, Jun, Jul,
Aug, Sep, Oct, Nov, Dec};
int main()
{
int i;
for (i=Jan; i<=Dec; i++)
printf("%d ", i);
return 0;
}
output:
0 1 2 3 4 5 6 7 8 9 10 11
//
// Another example program
#include <stdio.h>
enum State {Working = 1, Failed = 0};
int main()
{
printf("%d, %d", Working, Failed);
return 0;
}
output:
1, 0
//
/ Initialiaing an enum with typdef:
#include <stdio.h>
typedef enum month{ jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec } month; // define enum for month
//typedef struct date{ month m; int d} date; // struct for date
int main()
{
month m = feb;
printf("%d:", m);
return 0;
}
/ It will return: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment