Enum (enum) usage in C language

Source: Internet
Author: User

Recently, I used this concept in the generalized table of data structure, and did not pay much attention to it when I learned C language.

I combine a lot of information on the Internet, here to summarize their own.

First, there are enumerated types in both Java and C + +.


If you need several possible values for a variable, you can define it as an enumeration type. The enumeration means that it is possible to say that a variable or an object may exist, which can be said to be a value of one by one examples.
For example:

There is a pen in a pencil-box, but you do not know what pen it is, it may be a pencil or a pen before you open it.

There are two possibilities, so you can define an enumeration type to represent it!

Enum Box{pencil,pen};



This enumeration variable contains two elements, also known as enumeration elements, which are pencil and pens, respectively, that represent the pencil and pen.


If you want to define two variables with the same enumeration type of attributes then you can define them in two ways:

Enum Box{pencil,pen};    Enum Box box2;//or abbreviated as box Box2; There is another one that is defined at the time of the Declaration. enum {pencil,pen}box,box2; be defined at the same time as the declaration!

This looks like a structure;


Enumeration elements in enumeration variables the system is handled by constants, so called enumeration constants ,

They are not able to perform ordinary arithmetic assignments, (pencil=1;) Such a write is wrong,

But you can do the assignment at the time of declaration!

  Enum box{pencil=1,pen=2};

However, it is important to note that if you do not perform element assignment then the element will be automatically incremented from 0 for the assignment, and when it comes to automatic assignment, if you define only the first one then the system will add 1 to the value of the previous element for the next element.

For example:


Enum box{pencil=3,pen};//Here Pen is the 4 system will automatically perform pen=4 definition assignment operation!


Example:

Enum Type C language//Xin Yang # include <stdio.h> #include <stdlib.h>int main () {enum egg{a, B, c};enum egg test;  Test = A;                    The enumeration variable test is given an element operation, and if it is not assigned, the default is the last element,     switch (test) {case a:printf ("The value of test is a\n"), Break;case b:printf ("The value of test is B \ n "); Break;case c:printf (" The value of test is c\n "); return 0;}



C + + implementation:


#include <iostream>  using namespace std;    int main ()  {      enum egg {a,b,c};      Enum Egg test; Here you can abbreviate it into an egg test;        Test = C; The enumeration variable test is assigned to the element operation, where the assignment operation is not called assignment operation is to let everyone understand that the enumeration variable is not directly assigned to the arithmetic value, such as (test=1;) Such operations are not accepted by the compiler, the correct way is to first make a forced type conversion such as ( Test = (enum egg) 0;)!       if (test==c)       {          cout << enum Variable: The enumeration element corresponding to the test enumeration is C "<< Endl;       }        if (test==2)       {          cout << enum variable: The value of the test enumeration element is 2 "<< Endl;       }        cout << a << "|" << b << "|" << test <<endl;         Test = (enum egg) 0; Force type conversion     cout << "enumeration variable test value changed to:" << test <<endl;      Cin.get ();    return 0; }

The last thing you see here is that the enumeration elements in the enumeration variables (or enumeration constants) are automatically promoted to arithmetic types in special cases!

Enumeration types in real-world problems, the values of some variables are limited to a finite range. For example, there are only seven days in one weeks, 12 months in a year, six courses per week in a class, and so on. If these quantities are described as integers, the character type or other types are obviously inappropriate. For this reason, the C language provides a type called Enum. All possible values are enumerated in the definition of the "enum" type, and the variable value that is described as the "enum" type cannot exceed the defined range. It should be stated that an enumeration type is a basic data type, not a constructed type, because it can no longer be decomposed into any basic type. Description of the enumeration type's definition and enumeration variables 1. Enumeration defines the general form of an enumeration type definition: Enum Enumeration name {enumeration value table}; In the enumeration values table, all available values are listed in ROM. These values are also known as enumeration elements. The enumeration is named weekday, with a total of 7 enumerated values, which is seven days in a week. Any value that is described as a weekday type variable can only be one day in the seven day. 2. Enumeration variables are described as structs and unions, and enumeration variables can be described in different ways, namely, defining the post-description and defining the description or direct description. A variable a,b,c is described as the above weekday, which can be used in either of the following ways: Enum weekday{Sun,mou,tue,wed,thu,fri,sat}; Enum weekday a,b,c; or for: enum weekday{Sun,mou,tue,wed,thu,fri,sat}a,b,c; Or for: enum {Sun,mou,tue,wed,thu,fri,sat}a,b,c; 11.10. The assignment of a 2 enumeration type variable and the use of an enumeration type have the following provisions in use: 1. The enumeration value is a constant, not a variable. You cannot assign a value to an assignment statement in a program. For example, the elements of the enumeration weekday are then assigned the following values: sun=5; mon=2; Sun=mon; are all wrong. 2. The enumeration element itself is defined by the system as a numeric value representing the ordinal, from 0 to the beginning of the order defined as 0,1,2 .... As in weekday, the Sun value is 0,mon value of 1,...,sat value is 6. Main () {enum weekday {Sun,mon,tue,wed,thu,fri,sat} a,b,c; a=sun; b=mon; c=tue; printf ("%d,%d,%d", A,b,c)} Description: Only the enumeration value can be given to the The value of the element is not directly assigned to the enumeration variable. such as: a=sum; B=mon; is correct. and: a=0; B=1; Is wrong.If you must assign a value to an enumeration variable, you must cast with a coercion type. such as: A= (enum weekday) 2; The meaning is to assign the enumeration element of sequence number 2 to the enumeration variable A, which is equivalent to: a=tue; It should also be explained that the enumeration elements are not character constants or string constants, and do not use single or double quotes. Main () {enum body {a,b,c,d} month[31],j; int i; j=a; for (i=1;i<=30;i++) {month[i]=j; <span><span>j =   (</span><span class= "keyword" >enum</span><span> body)   (j + 1) ;  </span></span> if (j>d) j=a; } for (i=1;i<=30;i++) {switch (Month[i]) {case a:printf ("%2d%c/t", I, ' a '); break; case b:printf ("%2d%c/t", I, ' B '); Bre Ak Case c:printf ("%2d%c/t", I, ' C '); Break Case d:printf ("%2d%c/t", I, ' d '); Break Default:break; }} printf ("/n"); }



Enum (enum) usage in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.