Analysis of enum types in C + + and C language

Source: Internet
Author: User

1. First look at the C language enumeration type

The 1.c language defines an enumeration type, and each enumeration element is an integer
2. Focus on data type, no data type qualification
3. Adjacent enumeration element Difference integer
4. Can be accessed by integer, not secure enough

2. On the code:

1 #include <stdio.h> 2 #include <stdlib.h> 3  4/* 5  6 1.c The language defines the enumeration type, and each enumeration element is an integer 7 2. Focus on data type, no data type limited 8 3 . Adjacent enumeration elements differ by integer 9 4. Can be accessed through integers, not safe. */12 enum Color {black=10, white,yellow,red,blue,green};13-void{     Enum color c;17     c = red;18     printf ("%d\n", c);//1319     printf ("%d\n", white);//1120     C = 9;// Can still be used, can be run, but 9 is outside the enumeration range of     printf ("%d\n", c);//1322     System ("pause");    

3.C enumeration test code run result

4. See the C + + enumeration type again

The 1.c++ enumeration type qualifies the element type, which is the following: restricting the element to char type
2. Enumeration variables and assignments can be defined directly with the type name
3. Because of the type qualification, it is not possible to assign a value to the enumeration variable, only the elements in the value type can be assigned
4.c++ enumeration type is more secure

5. On the Code

1 #include <iostream> 2  3 using namespace std; 4  5//6 1.c++ Enumeration type qualifies element type, as follows to restrict an element to char Type 7 2. You can define a piece directly with a type name Lift variable and Assignment 8 3. Because of the type qualification, you cannot assign a value to an enumeration variable, only the element in the value type 9 4.c++ enum type more secure */11 enum Color:char {black= ' A ', white, yellow, red, bl UE, green};12 void {"* * * * * * *     red;//defined and initialized by     cout << MC << ENDL;//3, if there is black= ' a ' then the result is 681 7     //MC = 1;= ": Cannot convert from" int "to" color18     //MC = ' A '; ' = ': cannot convert from ' char ' to ' color '-     MC = color::white;//new syntax, value     cout << MC << endl;//1, if black= ' a ' The result is 6621 of     color C (red);//Initialize 323     cout << c << ENDL;//3, if there is black= ' a ' then the result is 6824     system (" Pause ");   

6. View running Results

7. It can be seen from the above that the maximum difference between C and C + + enumerations is the qualification of enumerated element types.

Analysis of enum types in C + + and C languages

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.