C Programming Language: Types, Structures, and Enumeration

Before getting into this subject, we first need to understand exactly what is a type in C.

What is a type?

“A type refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.”

yummy

Let’s say that every donut in the image above is a type. The chocolate one is an int, the one with the sprinkles are unsigned int, the one with white icing and sprinkles is a char, and so on… Every single one is different in some way from another one. The following table provides details of some of the standard types.

Now that we know about types, let’s jump into the enumerated types.

What is a structure?

A data structure is just the organization of data, in a particular way that can be used efficiently. Think of it as the deck of cards. Each card is an element from the structure (deck), in which the elements (card) has a given value. Something like this:

typedef struct s_deck
{
card2 = 2;
card3 = 3;
...
cardKing = 13;
cardAS = 14;
}

I know that each card is unique from each other in some way, such like 8th of Spade and 8th of Hearts; they have the same value, but different name, color, etc… For the sake of this tutorial, we just gonna cared about the value of the card (number).

What is an enumeration?

In simple terms, it’s an ordered list of all the items in a collection. It particulary refers to a listing of all of the elements of a set. The enumerated types consists of integral constants and each integral constant is give a name. Keyword enum is used to defined enumerated data type. For example:

#include <stdio.h>
enum week_days  
{
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday
};
int main(void)  
{
printf("Monday is the %dst day of the week.\n", monday);
printf("Thursday is the %dth day of the week.\n", thursday);
printf("Sunday is the %dth day of the week.\n", sunday);
return (0);
}

Here, we created an enumerated type called week_days, that have a list of the days of the week. Then we print out the numerical value of some days. For example, moday is the first day, tuesday is the second day, and so on... 
Compile the code above, and see its output.

Monday is the 0st day of the week.
Thursday is the 3th day of the week.
Sunday is the 6th day of the week.

Wait… what?

We seem to be a day of. Monday the 0st day? This is totally wrong! But wait… remember that real programmers count from zero ;) 
 So how do we fix this? Just add ‘+1’ at the end of the day (to increment its value by one), so 0 will be 1, and 1 will be 2…

printf("Monday is the %dst day of the week.\n", monday + 1);
printf("Thursday is the %dth day of the week.\n", thursday + 1);
printf("Sunday is the %dth day of the week.\n", sunday + 1);

Actually… I don’t like this. Sure, it’s “fixed”, but not really. What if I want to use a day somewhere else? I’ll need to keep adding ‘+ 1’ in every place, and the code will not be that readable. What can we do? Just assign the correspondeatat values inside the enumerated type! See the following snippet bellow:

enum week_days  
{
monday = 1,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday
};

Now try to compile it without the ‘+ 1’ inside the main function. Each element is assigned to a value, in this case an integer of values from 1 to 7, representing the respective order of the days.

Sources