aboutsummaryrefslogtreecommitdiffstats
path: root/test/aarch64/c/enum1.c
blob: d1f6b48d52450042878746c73daeaebf4eb08d66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* enum1.c  -- Starting to use enumerated types: Printing for each 
 *             day of the week, today, yesterday, and tomorrow, both
 *             as a string and as a number.
 */

#include <stdio.h>

/* Introducing an enumerated data type */
enum days {monday,tuesday,wednesday,thursday,friday,saturday,sunday};
typedef enum days days; // This allows us to use "days" as an abbreviation 
                        // for "enum days"

/* Two useful functions */
days yesterday(days today){
  return (today+6)%7;
}
days tomorrow(days today){
  return (today+1)%7;
}

// A useful array: thedays is an array of constant (i.e you cannot
// modify them) pointers to constant (i.e. you cannot modify them) strings
const char * const thedays[] = 
                      {"monday", "tuesday", "wednesday", "thursday",
		       "friday", "saturday", "sunday"};

int main(void){
  days today;

  printf("today    \tyesterday  \ttomorrow\n"
         "============================================\n");
  for (today=monday;today<=sunday;today++)
    printf("%s = %d \t %s = %d \t %s = %d\n", 
	   thedays[today], today, 
	   thedays[yesterday(today)], yesterday(today),
	   thedays[tomorrow(today)], tomorrow(today));
}

/*
 The output is:

today    	yesterday  	tomorrow
============================================
monday = 0 	 sunday = 6 	 tuesday = 1
tuesday = 1 	 monday = 0 	 wednesday = 2
wednesday = 2 	 tuesday = 1 	 thursday = 3
thursday = 3 	 wednesday = 2 	 friday = 4
friday = 4 	 thursday = 3 	 saturday = 5
saturday = 5 	 friday = 4 	 sunday = 6
sunday = 6 	 saturday = 5 	 monday = 0

*/