The enumeration of Dark Horse programmer--c Language

Source: Internet
Author: User

First, the concept of enumerations

Enumerations are a basic data type in the C language, not a constructed type, which can be used to declare a set of constants. When a variable has several fixed possible values, the variable can be defined as an enumeration type.

For example, you can use a variable of an enumeration type to represent the season, because there are only 4 possible values for the season: Spring, summer, autumn, and winter.

II. definitions of enumeration types

The general form is: Enum enumeration Name {Enumeration element 1, Enumeration element 2,......};

1 enum Season {Spring, summer, autumn, winter};

Third, the definition of the enumeration variable

The preceding just defines the enumeration type, and next you can define the variable with the defined enumeration type, which, like the struct, has 3 ways of defining the enumeration variable

1. Define the enumeration type first, and then define the enumeration variables
1 enum Season {Spring, summer, autumn, winter}; 2 3 enum Season s;

2. Defining enumeration variables while defining enumeration types 1 enum Season {Spring, summer, autumn, winter} s;

3. Omit the enumeration name and define the enumeration variables directly 1 enum {Spring, summer, autumn, winter} s;

All three of the above are defined as enumeration variables s

Iv. Considerations for enumeration use

The 1> C language compiler handles enumeration elements (spring, summer, and so on) As Integer constants, called enumeration constants.

The value of the 2> enumeration element depends on the order in which the enumeration elements are arranged when defined. By default, the first enumeration element has a value of 0, the second is 1, and sequentially adds 1.

1#include <stdio.h>2 3 intMain ()4 {5     //1. Defining Enum Types6     enumSeason7     {8 Spring,9 Summer,Ten Autumn, One Winter A     }; -      -     //2. Defining enumeration Variables the     enumSeason s =Winter; -      -      -printf"%d\n", s); +      -      +     return 0; A}


Print Result: 3

That is, spring has a value of 0,summer value of 1,autumn and a value of 2,winter is 3

3> can also change the value of an enumeration element when defining an enumeration type

1#include <stdio.h>2 3 intMain ()4 {5     //1. Defining Enum Types6     enumSeason7     {8Spring =1,9 Summer,Ten Autumn, One Winter A     }; -      -     //2. Defining enumeration Variables the     enumSeason s =Winter; -      -      -printf"%d\n", s); +      -      +     return 0; A}

Print Result: 4

An enumeration element with no value specified, with a value of 1 for the previous element.

V. Basic operations of enumeration variables 1. Assigning values

Enumeration constants or integer values can be assigned to enumeration variables

1#include <stdio.h>2 3 intMain ()4 {5     //1. Defining Enum Types6     enumSeason {Spring, summer, autumn, winter} s;7     8     //2. Defining enumeration Variables9s = Spring;//equivalent to s = 0;Tenprintf"%d\n", s); One     As = Winter;//equivalent to S = 3; -printf"%d\n", s); -      the     return 0; -}

Printing results 0

3

2. Traversing an enumeration element
Enum Season {Spring, summer, autumn, winter} s;//traverse the enumeration element for (s = spring; s <= Winter; s++) {    printf ("enum element:%d \ n", s );}

Output Result: Enumeration element: 0

Enumeration elements: 1

Enumeration elements: 2

Enumeration elements: 3

Summary of data types

I. Basic data types

1.int

1> long int, long:8 bytes%ld

2> short int, short:2 bytes%d%i

3> unsigned int, unsigned:4 bytes%zd

4> signed int, signed, Int:4 byte%d%i

2.float\double

1> Float:4 bytes%f

2> Double:8 bytes%f

3.char

1> 1 bytes%c%d

The 2> char type is stored in memory with its ASCII value

' A '-65

Ii. Types of construction

1. Arrays

1> can only be made up of the same type of data

2> definition: Data type array name [number of elements];

2. Structural body

1> can be made up of different types of data

2> define the type, then use the type to define the variable

Third, pointer type

1. Definition of variables

int *p;

2. Value of the indirect action variable

int a = 10;

p = &a;

*p = 20;

Iv. Enumeration Type

Application: When a variable is allowed to have only a few fixed values

The enumeration of Dark Horse programmer--c Language

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.