{"id":9244,"date":"2025-01-17T02:00:00","date_gmt":"2025-01-17T07:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=9244"},"modified":"2025-01-10T14:53:10","modified_gmt":"2025-01-10T19:53:10","slug":"using-enumerations-in-c","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=9244","title":{"rendered":"Using enumerations in C"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"9244\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><\/div>\n<p>With the C programming language, you have many options to arrange data in a program. For example, if you wanted to write a simple chess game, you might use <a href=\"https:\/\/www.both.org\/?p=9218\">bit fields and bit masks<\/a> to store both the type of piece and its color, for any square on the board:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef unsigned char square_t; \/* 1 byte *\/\n\n#define EMPTY  0\n#define PAWN   1\n#define ROOK   2\n#define KNIGHT 3\n#define BISHOP 4\n#define QUEEN  5\n#define KING   6\n\n#define BLACK  8\n#define WHITE  0\n\n#define PIECE  7<\/code><\/pre>\n\n\n\n<p>This makes the assumption that only three bits are needed to store the values 0 to 6, and a single bit (8) to store the color as either black or white. Using a bit mask of 7 (binary <code>111<\/code>) lets the program separate the value of the piece from its color. Such an implementation makes effective use of how computers store data as <em>bits<\/em> and <em>bytes<\/em>, but at the expense of making the program more difficult for human programmers to understand.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"let-the-computer-do-the-work\">Let the computer do the work<\/h2>\n\n\n\n<p>A more readable implementation leverages the <em>enumeration<\/em> feature in the C language. With enumeration, we leave the hard work to the computer. The compiler can figure out how to store the data most efficiently, and what values to assign. The programmer is left with <em>names<\/em> for values, which makes the source code more readable.<\/p>\n\n\n\n<p>In C, you use the <code>enum<\/code> keyword to set up an enumeration. For example, if you needed to track either <em>on<\/em> or <em>off<\/em> values in a single variable, you could do it with <code>enum<\/code> like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  enum {off, on} power;\n\n  power = on;<\/code><\/pre>\n\n\n\n<p>It doesn\u2019t matter to the programmer what the values are for <code>on<\/code> or <code>off<\/code>. Instead, the programmer only needs to know the <em>names<\/em> for these values. This makes the code more readable, such as a test if the power is turned on:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  if (power == on) {\n    puts(\"yes, it's ready\");\n  }\n  else {\n    puts(\"no, not ready\");\n  }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"updating-the-chess-board-with-enum\">Updating the chess board with \u2018enum\u2019<\/h2>\n\n\n\n<p>With enumeration, we can update the <a href=\"https:\/\/www.both.org\/?p=9218\">chess board program<\/a> to use <code>enum<\/code> instead of bit fields and bit masks. This makes the program both shorter and easier to read.<\/p>\n\n\n\n<p>The program was a simple representation of an 8&#215;8 chess board, and defined two pieces on the board: a black rook and a white pawn. To represent the different pieces that a player might put on a square, we first need to define an enumeration of those pieces:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  enum { empty, pawn, rook, knight, bishop, queen, king } piece;<\/code><\/pre>\n\n\n\n<p>With enumeration, it doesn\u2019t matter what the values are for each piece. We\u2019ll let the compiler do the \u201cheavy lifting\u201d for us, and assign the proper values for each entry in the enumerated list. In practice, enumerations usually start at zero, so <code>empty<\/code> is probably 0, <code>pawn<\/code> is 1, and so on. But the important part is that <em>we don\u2019t need to know the values<\/em>, because we can refer to each piece by its name: <code>king<\/code> and <code>queen<\/code> and so on.<\/p>\n\n\n\n<p>Similarly, we\u2019ll need to define an enumeration of the two possible colors:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  enum { black, white } color;<\/code><\/pre>\n\n\n\n<p>And to store both the piece and its color in one square on the chess board, we can use a <em>structure<\/em> like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct {\n  enum { empty, pawn, rook, knight, bishop, queen, king } piece;\n  enum { black, white } color;\n} square_t;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"putting-it-all-together\">Putting it all together<\/h2>\n\n\n\n<p>Let\u2019s combine everything to define an 8&#215;8 chess board with the two pieces on it. We can use the enumerations to store the piece and its color in each square on the board:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\ntypedef struct {\n  enum { empty, pawn, rook, knight, bishop, queen, king } piece;\n  enum { black, white } color;\n} square_t;\n\nint main()\n{\n  square_t board&#91;8]&#91;8]; \/* 0-7 is 8 squares, total is 8x8=64 squares *\/\n\n  board&#91;0]&#91;0].piece = rook;\n  board&#91;0]&#91;0].color = black;\n\n  board&#91;6]&#91;0].piece = pawn;\n  board&#91;6]&#91;0].color = white;\n\n  printf(\"the rook at &#91;0]&#91;0] is %s\\n\",\n    ( board&#91;0]&#91;0].color == black ? \"black\" : \"white\" ) );\n\n  printf(\"the pawn at &#91;6]&#91;0] is %s\\n\",\n    ( board&#91;6]&#91;0].color == black ? \"black\" : \"white\" ) );\n\n  return 0;\n}<\/code><\/pre>\n\n\n\n<p>If we compile and run this program, we\u2019ll see that the rook is black and the pawn is white:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ gcc -Wall -o chess chess.c\n\n$ .\/chess\nthe rook at &#91;0]&#91;0] is black\nthe pawn at &#91;6]&#91;0] is white<\/code><\/pre>\n\n\n\n<p>Using enumerations with <code>enum<\/code> makes it easy to store values in a program, yet keep the code easy to read. Enumerations are a good \u201ctool\u201d to add to your programmer\u2019s \u201ctool kit,\u201d alongside structures, bit fields, and bit masks. You\u2019ll find that the right tool at the right time can turn a difficult job into an easy one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using enumerations with enum makes it easy to store values in a program, yet keep the code easy to read.<\/p>\n","protected":false},"author":33,"featured_media":2814,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[150],"tags":[680,681,152],"class_list":["post-9244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-chess","tag-enumeration","tag-programming"],"modified_by":"David Both","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9244"}],"version-history":[{"count":1,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9244\/revisions"}],"predecessor-version":[{"id":9245,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/9244\/revisions\/9245"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/2814"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}