NetEase Cloud Classroom _c Language programming third week: Structure: structure, type definition, union

Source: Internet
Author: User

3.1 Enumeration

3.2 Structure

3.3 Type definition

3.1 Enumeration

An enumeration is a user-defined data type that is indicated by the keyword enum in the following syntax:

Enum Enum type name {name 0,..., name n};

Enumeration type names are usually not really used, but they are used in curly braces, because they are constant symbols, they are of type int, and the values are from 0 to n sequentially. Such as:

Enum colors{red, yellow, green};

Three constants are created, and the value of red is the value of 0,yellow is 1, and the value of green is 2

When you need some constant values that can be arranged, the meaning of the definition enumeration is to give the names of these constant values

Routines: enumeration of automatic counts

This is convenient when you need to iterate through all the enumerators or need to create an array that uses an enumerator to do the subscript.

1 #define_crt_secure_no_warnings2 3#include <stdio.h>4 5 enumColors6 {7Red, yellow, green, numcolors//enumeration of routines, the last number of colors to put8 };9 Ten Main () One { A     intcolor =-1; -     Char*colorsnames[numcolors] = {"Red","Yellow","Green" }; -     Char*colorname =NULL; the      -printf"Enter the code for the color you like:"); -scanf"%d", &color); -  +     if(Color >=0&& Color <numcolors) -     { +ColorName =Colorsnames[color]; A     } at     Else -     { -ColorName ="Unknown"; -     } -  -printf"The color you like is%s\n.", colorname); in  -System"Pause"); to}

Enumeration Amount

You can specify a value when declaring an enumerator

1 enum Colors 2 {3     1 5 4 };

Enum is just int

Even if you assign an integer value that does not exist to a variable of the enumerated type, there is no warning or error

Although enumeration types can be used as types, they are actually (BU) less (hao)

If there is a meaningful name for parallelism, it is easier to use enumerations than const INT

Enumeration Bihon (macro) Good, because enumerations have int types

3.2 Structure

3.3 Type definition

NetEase Cloud Classroom _c Language programming third week: Structure: structure, type definition, union

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.