Why is enum used in C language and enum used in C language?

Source: Internet
Author: User

Why is enum used in C language and enum used in C language?

Please indicate the source for reprinting; otherwise, legal liability will be held accountable.Http://blog.csdn.net/xingjiarong/article/details/47275971

In C, there is an enum keyword, which is an enumeration type. I don't know if it is usually used. However, the enum keyword is very useful in some cases. The following describes several situations where the enum keyword is used.

1. Define multiple constants at a time.

For example, if the problem is solved in our program, the day of the week may be converted to the number 1 on Monday, and the number 2 on Tuesday until the number 7, without the enum keyword, you can use define to define it, but you may feel very troublesome, because you need to define it one by one. The week is only 7 days. If it is a month, there are 12 months in a year, it is necessary to write 12 define statements, which is very inconvenient if enum is used.

#include<stdio.h>enum week {Mon=1,Tue,Wed,Thu,Fri,Sat,Sun};int main(){    printf("%d",Tue);    return 0;}

After this definition, the Mon value is 1, the Tue value is 2, the Wed value is 3, and so on.
Then you can use the defined 7 values like the constants after define.

If Mon = 1 is not entered at the beginning, the default value of Mon is 0, and then the growth starts from 0. For example:

enum color {red,blue,green,yellow};

If this is defined, the red value is 0, the blue value is 1, and then increases once.

If the value is assigned from the center:

enum color {red,blue,green=5,yellow};

Red to blue will grow from 0 by default, green is the defined value 5, and then the value after green will grow from 5.

Of course, you can also assign values to each enumerated variable. In this way, the definition of define is the same. If a value in the enumeration is not assigned a value, then it will change from the previous value assignment.
Volume, increase 1 at a time.

Ii. Define the scope of Variables

For example, in our application, to process things related to the month, it is clear that the month can only take a number from 1 to 12. To ensure the correctness and robustness of the program, we should use 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 defined enumerated type a can only be one of the 12 variables. If other variables are assigned, the compiler reports an error.

There are several methods to use enum:

1. Declare the variable while defining enum:

enum Month {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,dec} a,b;

In this way, two enumeration types a and B are declared.

2. Declare the variable after defining the enum:

enum Month {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,dec};enum Month a =  Feb;

3. Define anonymous enumerated Variables

enum  {Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,dec} a;

In this case, only a variable of the enumeration type can be used, and Other enumeration types cannot be defined.

Copyright Disclaimer: This article is the original author article, reprint please indicate the source, view the original article, please visit: http://blog.csdn.net/xingjiarong

Related Article

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.