Why to use Enum__c language in C language

Source: Internet
Author: User

Reprint Please indicate the source, otherwise will pursue the legal liability http://blog.csdn.net/xingjiarong/article/details/47275971

There is a keyword in C that is enum, enum type, do not know the usual use, but the enum keyword in some cases is very aspects, the following is the use of the enum keyword several situations.

一、一次 defines multiple constants.

For example, when dealing with a problem in our program, it is related to the week, you may want to convert Monday to the number 1, Tuesday to the number 2, until the number 7, without the enum keyword, you can use define to define it, but everyone will find it troublesome because you want a definition of one, week's okay to say , only 7 days, if the month, 12 months a year, that will write 12 define, very not, if the use of enum will be very convenient.

#include <stdio.h>

enum week {Mon=1,tue,wed,thu,fri,sat,sun};

int main ()
{
    printf ("%d", Tue);
    return 0;
}

After this definition, the value of the Mon value of 1,tue is 2,wed with a value of 3, one analogy.
You can then use the defined 7 values as if you were using the constants after define.

If you do not write mon=1 at the beginning, the default value for Mon is 0, and then the growth starts at 0. For example:

Enum color {Red,blue,green,yellow};

If this is the case, Red's value is 0,blue 1, and then it grows one at a time.

If you start assigning values from the middle:

Enum color {Red,blue,green=5,yellow};

So red to Blue is based on the default 0 growth, Green is the definition of the value of 5, and then the value of green after the start of the 5 growth.

Of course, you can also assign values to each enumerated variable, which is the same as all define definitions, and if there is a value in the enumeration that is not assigned, it will be a variable of the previous assignment.
The quantity begins, increases 1 at a time.

Ii. Scope of the limited variable

For example, in our application to deal with the month of things, it is obvious that the month can only take a number 1-12, in order to ensure the correctness and robustness of the program, we should use the enum.

#include <stdio.h>

enum Month {jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};

int main ()
{
    enum Month a =  Feb;
    printf ("%d", a);
    return 0;
}

For example, the definition of enum type A can only be one of those 12 variables, and if the other variables are given, the compiler will give an error.

There are several ways to use an enum:

1. Declare the variable while defining the enum:

Enum Month {jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec} a,b;

This declares two enumerated types A and B.

2, after defining the enum and then declaring the variable:

Enum Month {Jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec};
Enum Month a =  Feb;

3. Define anonymous enumeration variables

Enum  {Jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec} A;

In this case, you can only use a variable of this enumeration type of a, and no other enumeration type can be defined.

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.