Enumeration Simple Introduction

Source: Internet
Author: User

Note: The following all code execution Environment is VC + + 6.0

In a program, you might need to define an alias for some integers, and we can do this with the preprocessing directive # define, and your code might be:

#define MON 1
#define TUE 2
#define WED 3
#define THU 4
#define FRI 5
#define SAT 6
#define SUN 7

Here, we define a new type of data, and hopefully it will do the same. This new data type is called enum type.

1. Define a new data type-enum type

The following code defines this new data type-enum type

Enum Day
{
Mon=1, TUE, WED, THU, FRI, SAT, SUN
};

(1) An enum is a collection in which the elements (enumeration members) are named integer constants, separated by commas.

(2) Day is an identifier that can be regarded as the name of the collection, an optional , or optional item.

(3) The default value of the first enumeration member is 0 of the integer type, and the value of the subsequent enumeration member is added 1 on the previous member.

(4) The value of an enumeration member can be set artificially to customize an integer within a range.

(5) Enum type is an alternative to the pre-processing directive # define.

(6) Type definition with semicolons ; End.

2. Declaring a variable with an enumeration type

Once the new data type definition is complete, it is ready to use. We have seen the most basic data types, such as Integer int, single-precision float float, double-precision floating-point double, char char, short integer, and so on. Declaring variables with these basic data types is usually the case:

Char A; The type of variable A is character char
Char letter;
int x,
Y
Z The types of variables x, y, and z are int integers
int number;
Double m, N;
Double result; The type of the variable result is a double-precision floating-point type

Since an enumeration is also a data type, it can declare variables as well as basic data types.

method One: the definition of the enumeration type and the declaration of the variable are separated

Enum Day
{
Mon=1, TUE, WED, THU, FRI, SAT, SUN
};

Enum Day yesterday;
Enum Day Today;
Enum Day tomorrow; The type of the variable tomorrow is enum-enum day
Enum Day Good_day, Bad_day; The types of variables Good_day and BAD_DAY are enumerated enum day

method Two: type definition and variable declaration at the same time:

Enum//differs from the first definition, where the designator day is omitted, which is allowed.
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The type of the variable workday is enum-enum day

Enum Week {mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; The type of the variable days is enumerated enum week

Enum BOOLEAN {false, true} End_flag, Match_flag; Defines an enumeration type and declares two enum-type variables

method Three: use the typedef keyword to define an enumeration type as an alias and use that alias for variable declaration:

typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The workday here is the alias of enum enum workday

Workday today, tomorrow; Variables today and tomorrow are enumerated type workday, which is enum workday

The workday in enum workday can be omitted:

typedef enum
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The workday here is the alias of enum enum workday

Workday today, tomorrow; Variables today and tomorrow are enumerated type workday, which is enum workday

You can also use this method:

typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
};

Workday today, tomorrow; Variables today and tomorrow are enumerated type workday, which is enum workday

Note: An enumeration type with the same name cannot be defined in the same program, and a named constant with the same name cannot exist in a different enumeration type. The error examples are as follows:

Error declaration One: An enumeration type with the same name exists

typedef enum
{
Wednesday,
Thursday,
Friday
} workday;

typedef enum WEEK
{
Saturday
Sunday = 0,
Monday
} workday;

Error declaration two: An enumeration member with the same name exists

typedef enum
{
Wednesday,
Thursday,
Friday
} workday_1;

typedef enum WEEK
{
Wednesday,
Sunday = 0,
Monday
} workday_2;

3. Using variables of enum type

3.1 Assigns a value to the enumeration type variable.

An instance compares the assignment of an enumeration type with the assignment of a base data type:

method One: declare the variable first, then assign a value to the variable

#include <stdio.h>

/* Define enum type */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN};

void Main ()
{
/* Declare the variable using the base data type and assign a value to the variable */
int x, y, Z;

x = 10;
y = 20;
z = 30;

/* Declare the variable with an enumeration type, and then assign a value to the enum variable */
Enum Day yesterday, today, tomorrow;

Yesterday = MON;
Today = TUE;
Tomorrow = WED;

printf ("%d%d%d \ n", Yesterday, today, tomorrow);
}

method Two: assigning the initial value while declaring the variable

#include <stdio.h>

/* Define enum type */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN};

void Main ()
{
/* Use basic data type to declare variables and assign initial values to variables */
int x=10, y=20, z=30;

/* Declare a variable with an enumeration type and assign an initial value to the enumerated variable simultaneously */
Enum Day yesterday = MON,
Today = TUE,
Tomorrow = WED;

printf ("%d%d%d \ n", Yesterday, today, tomorrow);
}

method Three: declare the variable while defining the type, and then assign a value to the variable.

#include <stdio.h>

/* Define an enumeration type and declare three variables of that type, all of which are global variables */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN} Yesterday, today, tomorrow;

/* Define three variables with basic data types, all of which are global variables */
int x, y, Z;

void Main ()
{
/* Assign a variable to the base data type */
x = 10;  y = 20; z = 30;

/* Assign values to enumerated variables */
Yesterday = MON;
Today = TUE;
Tomorrow = WED;

printf ("%d%d%d \ n", x, Y, z); Output: 10 20 30
printf ("%d%d%d \ n", Yesterday, today, tomorrow); Output: 1 2 3
}

method Four: type definition, variable declaration, assign the initial value at the same time.

#include <stdio.h>

/* Define an enumeration type, declare three variables of that type, and assign an initial value. All of them are global variables */
Enum Day
{
Mon=1,
TUE,
WED,
THU,
FRI,
Sat
SUN
}
Yesterday = MON, today = TUE, tomorrow = WED;

/* Define three variables with a basic data type and assign an initial value. All of them are global variables */
int x = ten, y =, z = 30;

void Main ()
{
printf ("%d%d%d \ n", x, Y, z); Output: 10 20 30
printf ("%d%d%d \ n", Yesterday, today, tomorrow); Output: 1 2 3
}

3.2 When assigning an integer value to an enumerated variable, a type conversion is required .

#include <stdio.h>

Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN};

void Main ()
{
Enum Day yesterday, today, tomorrow;

Yesterday = TUE;
Today = (enum day) (yesterday + 1); Type conversions
Tomorrow = (enum day) 30; Type conversions
Tomorrow = 3; Error

printf ("%d%d%d \ n", Yesterday, today, tomorrow); Output: 2 3 30
}

3.3 Using the enum-type variable

#include <stdio.h>

Enum
{
BELL = ' \a ',
BACKSPACE = ' \b ',
Htab = ' \ t ',
RETURN = ' \ r ',
NEWLINE = ' \ n ',
vtab = ' \v ',
SPACE = '
};

Enum BOOLEAN {FALSE = 0, TRUE} match_flag;

void Main ()
{
int index = 0;
int count_of_letter = 0;
int count_of_space = 0;

Char str[] = "I ' m Ely efod";

Match_flag = FALSE;

for (; Str[index]! = ' + '; index++)
if (SPACE! = Str[index])
count_of_letter++;
Else
{
Match_flag = (enum BOOLEAN) 1;
count_of_space++;
}

printf ("%s%d times%c", Match_flag?) "Match": "Not Match", Count_of_space, NEWLINE);
printf ("Count of letters:%d%c%c", Count_of_letter, NEWLINE, RETURN);
}

Output:
Match 2 times
Count of Letters:10
Press any key to continue

4. Enumeration type and sizeof operator

#include <stdio.h>

Enum escapes
{
BELL = ' \a ',
BACKSPACE = ' \b ',
Htab = ' \ t ',
RETURN = ' \ r ',
NEWLINE = ' \ n ',
vtab = ' \v ',
SPACE = '
};

Enum BOOLEAN {FALSE = 0, TRUE} match_flag;

void Main ()
{
printf ("%d bytes \ n", sizeof (enum escapes)); 4 bytes
printf ("%d bytes \ n", sizeof (escapes)); 4 bytes

printf ("%d bytes \ n", sizeof (enum BOOLEAN)); 4 bytes
printf ("%d bytes \ n", sizeof (BOOLEAN)); 4 bytes
printf ("%d bytes \ n", sizeof (Match_flag)); 4 bytes

printf ("%d bytes \ n", sizeof (SPACE)); 4 bytes
printf ("%d bytes \ n", sizeof (NEWLINE)); 4 bytes
printf ("%d bytes \ n", sizeof (FALSE)); 4 bytes
printf ("%d bytes \ n", sizeof (0)); 4 bytes
}

5. General examples

#include <stdio.h>

Enum Season
{
Spring, summer=100, fall=96, winter
};

typedef enum
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;

void Main ()
{
/* Season */
printf ("%d \ n", spring); 0
printf ("%d,%c \ n", Summer, Summer); The D
printf ("%d \ n", fall+winter); 193

Season Myseason=winter;
if (Winter==myseason)
printf ("Myseason is winter \ n"); Myseason is winter

int x=100;
if (X==summer)
printf ("X is equal to summer\n"); X is equal to summer

printf ("%d bytes\n", sizeof (spring)); 4 bytes

/* Weekday */
printf ("sizeof Weekday is:%d \ n", sizeof (Weekday)); sizeof Weekday Is:4

Weekday today = Saturday;
Weekday tomorrow;
if (today = = Monday)
Tomorrow = Tuesday;
Else
Tomorrow = (Weekday) (today + 1); Remember to convert from int to Weekday
}

Enumeration simple Introduction (GO)

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.