Enums and Conversion Characters in C

Danny Padron
4 min readJan 22, 2021

--

What are “enums” or “conversion characters”, was my exact thought when I started my learning journey in C. We will get into detail on both of those terms and how they work in C and show some examples as well. But to start we need to understand how printf() works and why it is probably the most common function used in C.

printf() is a standard library function and outputs the information to the command line. Depending on what is put in between the () of the printf() function is what gets displayed to the command line. For example :

#include <stdio.h>int main()
{
//printf() displays the string inside quotation
printf("Hello, my name is Danny!");
return 0;
}

The output of this little snippet of code that will be displayed into the command line will be :

Hello, my name is Danny!

But not only can printf() print out simple strings but it can also display the values of variables and even the results of computations. Now that we know the basic capabilities of printf() we are going to use it with our “Enums” and “Conversion Characters”.

Enums

An enumeration (or enum) is a data type that is defined by the user and the valid values that could be stored within that enum are also defined. So in simple, an enum is a variable that is defined with certain values and only those defined values for that specific enum can be stored within that enum itself. To declare an enum within your C program you first have to define the enum by giving it a name, which is initiated by the keyword enum . Then followed by the list of identifiers that define the permissible values that can stored within that enum. For example :

#include <stdio.h>int main()
{
// create the enum with a type name 'primaryColors' and define the valid values that can be used for that enum
enum primaryColors { red, blue, green, black, white, gray, yellow, pink } //Display the values
printf("The value of red is: %d\n", red);
printf("The value of black is: %d\n", black);
return 0;
}

In the example above we create the enum and define the valid identifiers. enum primaryColors { red, blue, green, black, white, gray, yellow, pink } We use the enum keyword to initialize followed by the name of the enum then within the curly braces is where we store the identifiers. The C compiler treats enumeration identifiers as integer constants. So really red is 0, blue is 1, green is 2 and so on. The example above also shows how we would display the values of those identifiers. This is where Conversion Characters come into play.

Conversion Characters

The true power of the printf() function is the formatting and the ability to specifically display values. Conversion Characters are the %d that are seen in the example and there are many others depending on what you want displayed and how you want it displayed. In C integer values can either be signed (a variable that can hold a negative or positive value) or unsigned (a variable that can hold only positive values). The main conversion character used for signed variables is %d and %u for unsigned variables. If you had a float type integer you would use %f . There is even conversion characters that you could give for either long integers or even if you wanted to limit how many characters that long integer actually displays on the command line. The above example :

//Display the values 
printf("The value of red is: %d\n", red);
printf("The value of black is: %d\n", black);

The %d indicates that it is a signed integer that will be displayed and the \n just creates a new a line after displaying the value of this printf() function. So the displayed solution would look like this :

The value of red is: 0
The value of black is: 3

The value of red is 0 because it is the first of the listed identifiers which starts at 0 then 1 and so on. But you can also change the value of these identifiers by defining them when creating the enum. So for example you could create an enum that looks like this :

#include <stdio.h>int main()
{
enum primaryColors { red= 3, blue, green=9, black, white, gray, yellow, pink }
printf("The value of red is: %d\n", red);
printf("The value of black is: %d\n", black);
return 0;
}

What do you think the printed out display will look like this time? We created the same enum we only set the value of 2 of the identifiers. The printed out display would look like this :

The value of red is: 3
The value of black is: 10

Because we defined the identifier red as 3 it has the value of 3 now instead of the original 0. Changing the identifier green to 9 as well changes the values of the following identifiers because it will continue to count on from the newly defined identifier value. So black becomes 10 and white becomes 11 and so on.

Between the printf() function and the use of enum and ‘Conversion Characters’, these 3 things in C alone can give you a lot of possibilities on what these could be used for on bigger more complex projects written in C.

--

--

Danny Padron
Danny Padron

Written by Danny Padron

Full stack web developer with experience in Ruby on Rails, JavaScript, React and Redux and learning more!

No responses yet