C Language enumeration type (enum) detailed and sample code _c language

Source: Internet
Author: User

In practical programming, some data is often limited, only a very small number of integers, and preferably a name for each value to facilitate the use of subsequent code, such as one weeks only seven days, only 12 months a year, a class has six courses each week.

For example, seven days a week, we can use the #define command to specify a name for each day:

#include <stdio.h>
#define MON 1
#define Tues 2
#define WED 3
#define THURS 4
#define FRI 5
   
     #define SAT 6
#define SUN 7
int main () {
  int day;
  scanf ("%d", &day);
  Switch (day) {case
    mon:puts ("Monday");
    Case Tues:puts ("Tuesday"); break;
    Case Wed:puts ("Wednesday"); break;
    Case Thurs:puts ("Thursday"); break;
    Case Fri:puts ("Friday"); break;
    Case Sat:puts ("Saturday"); break;
    Case Sun:puts ("Sunday"); break;
    Default:puts ("error!");
  }
  return 0;
}

   

Run Result:

5↙
Friday

#define命令虽然能解决问题, but also brought a lot of side effects, resulting in too many macro names, loose code, looks always a bit uncomfortable. The C language provides an enumeration (enum) type that can list all possible values and give them a name.

Enumeration types are defined in the form:

Enum typename{valueName1, valueName2, ValueName3, ...};

An enum is a new keyword that is specifically used to define an enumeration type, which is its only use in the C language; TypeName is the name of the enumeration type;

ValueName1, ValueName2, ValueName3, ... is a list of the names that correspond to each value. Pay attention to the last;

For example, list one weeks for a few days:

Enum week{Mon, Tues, Wed, Thurs, Fri, Sat, Sun};

As you can see, we just give the name, but not the name of the corresponding value, because the enumeration value by default starting from 0, and then add 1 (increment), that is, week Mon, Tues ... Sun's corresponding values are 0, 1, respectively ... 6.

We can also specify a value for each name:

Enum week{Mon = 1, tues = 2, Wed = 3, thurs = 4, Fri = 5, Sat = 6, Sun = 7};

The simpler approach is to specify a value for only the first name:

Enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun};

The enumeration value is incremented from 1, which is equivalent to the above notation.

An enumeration is a type that allows you to define an enumeration variable:

Enum week A, B, C;

You can also define a variable while defining an enumeration type:

Enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun} A, b, C;

With an enumeration variable, you can assign the value in the list to it:

Enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun};
Enum Week A = Mon, B = Wed, c = Sat;

Or:

Enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun} a = Mon, B = Wed, c = Sat;

The example determines how many days of the week the user enters.

#include <stdio.h>
int main () {
  enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun} day;
  scanf ("%d", &day);
  Switch (day) {case
    mon:puts ("Monday");
    Case Tues:puts ("Tuesday"); break;
    Case Wed:puts ("Wednesday"); break;
    Case Thurs:puts ("Thursday"); break;
    Case Fri:puts ("Friday"); break;
    Case Sat:puts ("Saturday"); break;
    Case Sun:puts ("Sunday"); break;
    Default:puts ("error!");
  }
  return 0;
}

Run Result:

4↙
Thursday

The two points to note are:

1 enumeration List of Mon, Tues, Wed The scope of these identifiers is global and no longer defines the same variables as their names.

2) Mon, Tues, Wed, etc. are constants that cannot be assigned values and can only be assigned to other variables.

Enumerations and macros are actually very similar: macros replace names with corresponding values during the preprocessing phase, and enumerations replace names with corresponding values during the compile phase. We can interpret the enumeration as a compilation-phase macro.

For the above code, at some point in the compilation, it will look something like the following:

#include <stdio.h>
int main () {
  enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun} day;
  scanf ("%d", &day);
  Switch (day) {case
    1:puts ("Monday");
    Case 2:puts ("Tuesday"); break;
    Case 3:puts ("Wednesday"); break;
    Case 4:puts ("Thursday"); break;
    Case 5:puts ("Friday"); break;
    Case 6:puts ("Saturday"); break;
    Case 7:puts ("Sunday"); break;
    Default:puts ("error!");
  }
  return 0;
}

The names of Mon, Tues and Wed are replaced by the corresponding numbers. This means that Mon, Tues, Wed, etc. are not variables, they do not occupy the data area (constant area, global data area, stack area and heap area) of memory, but are directly compiled into the command, put in the code area, so can not use & to obtain their address. This is the nature of the enumeration.

With regard to the partition of the program in storage and the role of each partition, we will explain in detail in the section "Memory layout (memory model)" of the Linux C language program in the "C Language Memory" topic.

As we have said in the "C" switch statement section, the case keyword must be followed by an integer, or an expression that results in an integer, but cannot contain any variables, and it is because Mon, Tues, Wed These names are eventually replaced with an integer, so they can be placed behind the case.

struct variables need to be stored in an integer, I guess its length and int should be the same, the following to verify:

#include <stdio.h>
int main () {
  enum week{Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun} day = Mon;
  printf ("%d,%d,%d,%d,%d\n", sizeof (Enum week), sizeof (day), sizeof (Mon), sizeof (Wed), sizeof (int));
  return 0;
}

Run Result:

4, 4, 4, 4, 4

The above is the C language enumeration type of data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.