C language basic syntax - structure, union and enumeration

  1. Structure

  1. What is a structure

  2. Structure syntax format

  3. The memory space occupied by the structure

  4, structure member assignment

  2. United

  1. What is union

  2. Combined grammar format

  3. Enumeration

  1. What is enumeration

  2. Enumeration syntax format

 

1. Structure

  1. What is a structure

  • is a data type

  • Programmer-defined data types

  • Structures can contain members of different types

  2. Structure syntax format

struct{

  member;

}variable name;

typedef struct {
  int age;

  char name[20];

}Student2;//Alias

  3. The memory space occupied by the structure

  • Character alignment rules

- Find the member that occupies the most storage space

- Allocate storage space in its unit

- Each member is stored at the offset where the remainder of the number of bytes occupied by the member is 0

  4, structure member assignment

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;

}

 

2. United

  1. What is union

  • The usage, syntax, and structure of unions are very similar, but all members of the union allocate the same block of memory. (Only one member information can be saved, and the space of the union is based on the space occupied by the largest member)

  • Unions can use a single memory for multiple data types

  • The difference between union and structure, structure can store multiple member information, while union can only store one member information and the last one.

  2. Combined grammar format

typedef union {

  int age;

  char name[2];

}LianHe;

  

3. Enumeration

  1. What is enumeration

  • Use letters to describe a regular set of values.

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

  • The enumeration value can only be modified when the enumeration is declared

  • 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;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529799&siteId=291194637