C Basic syntax-structs, unions, and enumerations

Source: Internet
Author: User

First, the structural body

1, what is the structure of the body

2. Structure Syntax format

3, the structure of the occupied memory space

4, the structure member assigns the value

Ii. United

1, what is the Union

2. Union syntax format

Third, enumeration

1. What is Enumeration

2. Enumeration Syntax format

First, the structural body

1, what is the structure of the body

? is a data type

? Data types that are customized by the programmer

  ? The structure body can contain different types of members

2. Structure Syntax format

struct{

Members

} variable name;

typedef struct {
int age;

Char name[20];

}student2;//aliases

3, the structure of the occupied memory space

? Character Alignment rules

-Identify the members that occupy the largest storage space

-Allocate storage space as a unit

-Each member is stored at an offset of 0 of the number of bytes taken by the member

4, the structure member assigns the value

int main (int argc, const char * argv[]) {

struct{

int i;

Char ch;

Double D;

}s;

S.I = 10;

s.ch = ' A ';

S.D = 3.14;

printf ("%d,%c,%g\n", S.i, s.ch, S.D);

return 0;

}

Ii. United

1, what is the Union

? The usage, syntax, and structure of the Union are very similar, but the memory allocated by all members of the Union is the same block. (Only one member information can be saved, and the combined space is the value of the maximum member space)

? A union can use a piece of memory to correspond to multiple data types

? The union differs from the structure in that the structure can hold multiple member information, and the Union can only hold one member information and the last.

2. Union syntax format

typedef Union {

int age;

Char name[2];

}lianhe;

  

Third, enumeration

1. What is Enumeration

? Use letters to describe a set of regular values.

? The default value of the enumeration starts at 0, and each value is an integer constant

  ? You can modify an enumeration value only when declaring an enumeration

? Modified enumeration value = Previous enumeration value plus 1

2. Enumeration Syntax format

-enum Week {MONDAY, Tuesday, Wednesday, Thursday, FRIDAY, SATURDAY, SUNDAY};

-typedef enum {FALSE, TRUE} BOOL;

C Basic syntax-structs, unions, and enumerations

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.