{"id":1440,"date":"2015-07-11T00:01:47","date_gmt":"2015-07-11T07:01:47","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1440"},"modified":"2015-07-04T08:51:50","modified_gmt":"2015-07-04T15:51:50","slug":"the-mysterious-enum-keyword","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1440","title":{"rendered":"The Mysterious <em>enum<\/em> Keyword"},"content":{"rendered":"<p>They&#8217;re the orphan keywords, urchins, unwanted, unused, unloved. Of the 32 C language keywords, a handful are seldom used. These include: <em>auto<\/em>, <em>enum<\/em>, <em>register<\/em>, <em>union<\/em>, and <em>volatile<\/em>.<br \/>\n<!--more--><br \/>\nA few others are used sparingly, such as <em>const<\/em> and <em>static<\/em>. I won&#8217;t even get into the underlined keywords added with the C11 standard.<\/p>\n<p>Of all the orphans, the one I keep coming back to is <em>enum<\/em>. For some reason it piques the curiosity of many programmers, wondering what the heck it actually does.<\/p>\n<p>In my code, I&#8217;ve used <em>enum<\/em> once. It provided an elegant solution to a simple problem and helped make the code more readable.<\/p>\n<p>The word <em>enum<\/em> is short for <em>enumerate<\/em>, which is the process of assigning numbers to something or listing out specific items. In the C language, <em>enum<\/em> creates a list of constants, each of which is assigned a value used elsewhere in the code.<\/p>\n<p>What <em>enum<\/em> doesn&#8217;t do is to create an associative array. It doesn&#8217;t create an array at all. It creates constants, similar to the <em>define<\/em> directive. Unlike <em>define<\/em>, <em>enum<\/em> builds a slew of constants at once, each numbered sequentially. Here&#8217;s the format:<\/p>\n<pre><code>enum {const1, const2, ...};<\/code><\/pre>\n<p>The <em>enum<\/em> keyword creates constants <code>const1<\/code>, through whatever. The format is similar to an array, but the items listed become constants. I typically use ALL CAPS for the names, but that&#8217;s not a rule.<\/p>\n<p>Values assigned to the constants start with 0 and increment by 1. The following <em>enum<\/em> statement is popular in a few programs:<\/p>\n<p><code>enum {FALSE,TRUE};<\/code><\/p>\n<p>This statement sets the value of constant <code>FALSE<\/code> to zero and <code>TRUE<\/code> to one. Here&#8217;s sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    enum {FALSE,TRUE};\r\n\r\n    printf(\"FALSE is %d\\n\",FALSE);\r\n    printf(\"TRUE is %d\\n\",TRUE);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>FALSE is 0\r\nTRUE is 1<\/code><\/pre>\n<p>The effect is the same as if I&#8217;d used:<\/p>\n<pre><code>#define FALSE 0\r\n#define TRUE 1<\/code><\/pre>\n<p>One major difference between <em>enum<\/em> and <em>define<\/em> is where you find these statements in the code. I typically use <em>define<\/em> externally so that the constants are available throughout the code. You can use <em>enum<\/em> that way, but when you place it inside a function, the constants are available only inside that function.<\/p>\n<p>Unless you specify otherwise, <em>enum<\/em> constants start at value 0 and increase by 1 for each constant. You can set new values by using the <code>=<\/code> operator. For example:<\/p>\n<p><code>enum {ALPHA=1,BETA,GAMMA};<\/code><\/p>\n<p>Above, <code>ALPHA<\/code> is set to value 1, then <code>BETA<\/code> would be 2 and <code>GAMMA<\/code> 3. Or you could do this:<\/p>\n<p><code>enum {ALPHA=1,BETA=5,GAMMA};<\/code><\/p>\n<p>Above, <code>ALPHA<\/code> is set to value 1, then <code>BETA<\/code> is set to 5. <code>GAMMA<\/code> would be equal to 6.<\/p>\n<p>Of course, if you&#8217;re going to set individual constant values, just use <em>define<\/em> instead of <em>enum<\/em>.<\/p>\n<p>Here&#8217;s another example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    enum {ALPHA=1,BETA,GAMMA};\r\n    int test;\r\n\r\n    printf(\"Which test is this? (1-3): \");\r\n    scanf(\"%d\",&test);\r\n\r\n    switch(test)\r\n    {\r\n        case ALPHA:\r\n            puts(\"Alpha test\");\r\n            break;\r\n        case BETA:\r\n            puts(\"Beta test\");\r\n            break;\r\n        case GAMMA:\r\n            puts(\"Gamma test\/release\");\r\n            break;\r\n        default:\r\n            puts(\"Unknown test number\");\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Because <em>enum<\/em> sets the constant <code>ALPHA<\/code> to 1, the <code>test<\/code> variable set at Line 9 matches the enumerated constant values.<\/p>\n<p>If you can use <em>enum<\/em> in your code, great. You&#8217;ll join the ranks of those few C coders who understand and appreciate how it works. Otherwise, it ain&#8217;t no big deal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s completely possible to enjoy a successful programming career without ever using this keyword. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1440\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1440","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1440"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1440\/revisions"}],"predecessor-version":[{"id":1451,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1440\/revisions\/1451"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}