Uncover the veil of the enumeration class (unlocking the Enumeration/enum Mystery)

Source: Internet
Author: User
Tags tagname

Enumeration is convenient for user-defined fixed data groups. An enumeration class is a series of constant integer values, which means that the enumeration type cannot be modified.

Here we will discuss the usage and limitations of enum types in C.

Enumerations are defined by enumerating key values, similar to struct definitions

Syntax (Syntax):

enum tagname{const1=1, CONST2,...} enumvariable;

tagname, enumeration type name, this item can be omitted.

{Const1=1,const2,...} A set of strings that replace constant values, each corresponding to an integer data, the shaping data starting at 0, the second being 1, and so on. The ordinal of a string is the integer value of the string, unless the user defines the class as const1=1. If the user specifies the integer value of the string itself, then the corresponding value of the string that follows it is followed by the successive values. That's not clear, for example, Const2=6, the value of the element behind this element is the corresponding 7,8,9, and so on.

enumvariable A list of variables for the enumeration type. The variables of the enumeration type are integral types, except for enumeration constants, which can be assigned to any integer data. This definition is also optional, like a struct, or it can be defined in a later program:

enum tagname enumvariable;

The following is an example of enumeration type usage:

#include <stdio.h>//defining the enumenumWeek{monday= -1, tuesday,wednesday,thursday=6, friday,saturday,sunday}; intMainvoid){    //declaring an enum in the program    enumWeek day=FRIDAY; Switch(day) { Casemonday:printf ("Monday Sucks");  Break;  Casesaturday:printf ("Weekends is fun");  Break;  Casesunday:printf ("Weekends is fun");  Break; default: printf ("It depends on the mood:D%d", day);  Break; }         return 0;}

Uncover the veil of the enumeration class (unlocking the Enumeration/enum Mystery)

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.