



{"id":4547,"date":"2018-08-31T01:25:44","date_gmt":"2018-08-30T19:55:44","guid":{"rendered":"http:\/\/computersirkiclass.com\/?p=4547"},"modified":"2026-03-18T18:43:26","modified_gmt":"2026-03-18T13:13:26","slug":"concept-of-enumeration-in-c","status":"publish","type":"post","link":"https:\/\/computersirkiclass.com\/cskc\/concept-of-enumeration-in-c\/","title":{"rendered":"Concept of Enumeration in C++"},"content":{"rendered":"<div id=\"modal-ready\"><p>Enumeration is basically a way to represent a possible list of number values for a variable in friendly names or symbol forms. For e.g. if\u00a0 you have to represent rainbow colours with 0 to 6 you can always write them more meaningfully as<\/p>\n<p>enum rainbow { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET };<\/p>\n<p>This will essentially give values from 0 to 6 to given colours in that order. so if following code is written &#8211;<\/p>\n<pre>enum rainbow { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET };\r\nrainbow colour;\r\ncolour = YELLOW;\r\ncout&lt;&lt;colour;<\/pre>\n<p>Then output would be 2 as YELLOW is the third variable and its value would be integer 2 automatically.<\/p>\n<h3>Observation while using enumerated data types:<\/h3>\n<p>Please make following observations in the above declaration and usage in code.<\/p>\n<ul>\n<li>The declaration begins with prefix <strong>enum<\/strong> with a user defined data type name which in this case is <strong>rainbow<\/strong>.<\/li>\n<li>The declaration contains friendly value names separated by comma in a structure or class like declaration with curly braces and following semicolon.<\/li>\n<li>When used it follows a simple syntax of datatype name followed by a variable name which in this case is <strong>rainbow colour;<\/strong><\/li>\n<li>The assigned variable can now be assigned only a value from within the given value list range only. If any thing else is assigned to variable name then compiler error will return. For e,g.<br \/>\ncolour = YELLOW; is correct<br \/>\ncolour=MAROON; is not correct as it is not there in the symbol list defined.<br \/>\ncolour=2; will also not be correct as it may be internally assigned value but not present as a symbol in the given symbol list.<\/li>\n<li>Internally the symbol names in the list gets converted to values and when printed they print the value only.<\/li>\n<li>Enumeration may allow user given fixed values like &#8211;<br \/>\nenum rainbow { RED=1, ORANGE=2, YELLOW=3, GREEN=4, BLUE=5, INDIGO=6, VIOLET=7 };<\/li>\n<\/ul>\n<h3>Advantages of Enumeration:<\/h3>\n<ul>\n<li>Allows to check for fixed set of values only for any variable.<\/li>\n<li>Allows to provide friendly names to list of values.<\/li>\n<li>The default values for value list is generated automatically from 0 to further as per number of items in the list. (This is converse to #define where providing values is a must)<\/li>\n<li>Is a good technique to assign multiple value combination as OR or AND form to the same variable.<\/li>\n<li>Additional value settings can be provided to a variable easily.<br \/>\n<a href=\"https:\/\/www.programiz.com\/cpp-programming\/enumeration\" target=\"_blank\" rel=\"noopener\">Watch Few Examples Here<\/a><\/li>\n<\/ul>\n<h3>Some Examples of Enumerated Data Types<\/h3>\n<ul>\n<li>enum season{spring, summer,autumn,winter};<\/li>\n<li>enum gender{male,female,other};<\/li>\n<li>enum weekdays{monday,tuesday,wednesday,thursday,friday,sunday};<\/li>\n<li>enum cards{club,diamond,heart,spade};<\/li>\n<li>enum artist_primary_colours{RED, YELLLOW, BLUE};<\/li>\n<li>enum direction{east,west,north,south};<\/li>\n<li>enum error{Memory_Err,File_err,Hardware_Err,Communication_Err};<\/li>\n<li>enum session{morning,evening};<\/li>\n<li>enum boolean{false,true};<\/li>\n<li>enum attribs{bold,underline,italic};<\/li>\n<li>enum G7{Canada,France,Germany,Italy,Japan,UK,USA,EU};<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Enumeration is basically a way to represent a possible list of number values for a variable in friendly names or symbol forms. For e.g. if\u00a0 you have to represent rainbow colours with 0 to 6 you can always write them more meaningfully as enum rainbow { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET }; This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7767,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[114,99,98],"tags":[233],"model_paper":[],"class_list":["post-4547","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cnr","category-cpp","category-cpp-all","tag-cp320-enumerated-types","cpp_index-cp7040-enumerated-types"],"acf":[],"_links":{"self":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/comments?post=4547"}],"version-history":[{"count":5,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4547\/revisions"}],"predecessor-version":[{"id":4553,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/posts\/4547\/revisions\/4553"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/media\/7767"}],"wp:attachment":[{"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/media?parent=4547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/categories?post=4547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/tags?post=4547"},{"taxonomy":"model_paper","embeddable":true,"href":"https:\/\/computersirkiclass.com\/cskc\/wp-json\/wp\/v2\/model_paper?post=4547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}