{"id":5257,"date":"2023-03-10T12:55:24","date_gmt":"2023-03-10T12:55:24","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=5257"},"modified":"2023-03-10T12:58:25","modified_gmt":"2023-03-10T12:58:25","slug":"enum-in-cpp","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/enum-in-cpp\/","title":{"rendered":"Enumeration in C++"},"content":{"rendered":"<p>In this tutorial, we are going to learn about the concept of Enumeration, or enum, which is a datatype, and an interesting concept. We will see an example, which demonstrates the concept so that it becomes easy to understand for you, and you will be comfortable using the concept in your C++ programs.<\/p>\n<h2 style=\"text-align: center;\">Enumeration in CPP<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5258 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/Enumeration-in-CPP.jpg\" alt=\"\" width=\"640\" height=\"480\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/Enumeration-in-CPP.jpg 640w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/Enumeration-in-CPP-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>Enumeration is a kind of user-defined data type, which consists of named values, which represent integral constants. We will understand this with help of an example ahead. In order to define an enumeration, we have to use the &#8220;<strong>enum<\/strong>&#8221; keyword.<\/p>\n<h3>Enum in C++ Example<\/h3>\n<p>Have a look at a simple example, through which, we can understand the concept of enumeration-<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5259 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/enum-in-CPP.png\" alt=\"\" width=\"953\" height=\"514\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/enum-in-CPP.png 953w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/enum-in-CPP-300x162.png 300w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/enum-in-CPP-768x414.png 768w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><\/p>\n<p>output &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5260 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/enum-in-CPP-output.png\" alt=\"\" width=\"441\" height=\"104\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/enum-in-CPP-output.png 441w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2023\/03\/enum-in-CPP-output-300x71.png 300w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><\/p>\n<p>So, as you can see that we created a variable of type month, which is an enumeration. By default, the first value in the enum has a value of 0, but here I made it 1 to just show that you can modify those values too&#8230; you can try putting 90 to July, and you will see that August will now have a value of 91. If you do not assign anything to January, then it would be zero, and the current month variable which contains the value of March would have a value of 2(instead of 3).<\/p>\n<p>In simple words, we can understand enumeration as a data type, which consists of named values, which represent integral constants, just like in the above example, January represents 1, February represents 2, and so on.<\/p>\n<h4>Summary<\/h4>\n<p>In this tutorial, we learned about enum or enumeration in C++. The concept is very interesting and useful at times when we need to have named values representing integral constants. We saw an example, which demonstrates the concept, and its application. <\/p>\n<p>You can also try some more programs, and some more use cases, for the enumeration.<\/p>\n<p>If you find this tutorial to be interesting, you can explore our other tutorials related to C++ programming language to learn more about C++ programming language. You can also explore our other courses, related to <a href=\"https:\/\/gyanipandit.com\/programming\/what-is-python\/\">Python<\/a>, <a href=\"https:\/\/gyanipandit.com\/programming\/what-is-machine-learning\/\">Machine learning<\/a>, and Data Science, through which, you can upskill yourself.<\/p>\n<h4>FAQs related to enum in c++<\/h4>\n\n\n<div class=\"wp-block-rank-math-faq-block\"><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What is an enum in C \/ C++?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> In C \/ C++, an enum is a datatype, which consists of named values, which represent integral constants.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: How can we create an enum in C++?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> To create an enum in C++, we need to use the keyword enum.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: Is an enum a keyword in C++?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> Yes, &#8220;enum&#8221; is a keyword in C++, used to declare new enumeration types in programs.<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to learn about the concept of Enumeration, or enum, which is a datatype, and an interesting concept. We will see an example, which demonstrates the concept so that it becomes easy to understand for you, and you will be comfortable using the concept in your C++ programs. Enumeration in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5258,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[40,136,146,147],"class_list":{"0":"post-5257","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-c","8":"tag-c","9":"tag-cpp","10":"tag-enumeration-in-c","11":"tag-enumeration-in-cpp"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=5257"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5257\/revisions"}],"predecessor-version":[{"id":5264,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5257\/revisions\/5264"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media\/5258"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=5257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=5257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=5257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}