Enumeration and structure in C language

enumeration, structure

Enumeration enum

  • In the C language, enumerated types are treated as int or unsigned int types.

  • Continuous enumeration variables can be traversed, and discontinuous enumeration variables can be conditionally traversed using switch.

  • Enum is equivalent to defining multiple related macros with #define.

  • The default value of the first enumeration member is an integer of 0, and the value of subsequent enumeration members is added to the previous member by 1;

  • If the value of an enumeration member is specified, its subsequent member is equal to the value of the previous member + 1;

  • Definition and use

// 1、先定义枚举类型,再定义枚举变量
// 定义(枚举类型时改变枚举元素的值:)
enum DAY
{
    
    
    MON = 1,
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN
};

enum DAY day;

// 2、定义枚举类型的同时定义枚举变量
// enum DAY {
    
    
//     MON = 1,
//     TUE,
//     WED,
//     THU,
//     FRI,
//     SAT,
//     SUN
// } day;
  • All examples:
#include <stdio.h>
// 1、先定义枚举类型,再定义枚举变量
// 定义(枚举类型时改变枚举元素的值:)
enum DAY
{
    
    
    MON = 1,
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN
};

int main()
{
    
    
    enum DAY day;
    day = MON;
    printf("%d\n\n", day);

    //在C 语言中,枚举类型是被当做 int 或者 unsigned int 类型来处理的
    printf("遍历枚举元素:\n");
    for (int i = MON; i <= SUN; i++)
        printf("%d\n", i);
    puts("");

    /* 用户输入数字来选择颜色 */
    printf("今天是星期几: (1. MON, 2. TUE, 3. WED, 4.THU, 5. FRI, 6. SAT, 7. SUN): ");
    scanf("%u", &day);
    switch (day)
    {
    
    
    case MON:
        printf("今天是星期一.\n");
        break;
    case TUE:
        printf("今天是星期二.\n");
        break;
    case WED:
        printf("今天是星期三.\n");
        break;
    case THU:
        printf("今天是星期四.\n");
        break;
    case FRI:
        printf("今天是星期五.\n");
        break;
    case SAT:
        printf("今天是星期六.\n");
        break;
    case SUN:
        printf("今天是星期天.\n");
        break;

    default:
        printf("输入内容错误.");
        break;
    }

    return 0;
}

structure

typedefBehaves a bit like a #define macro, substituting a synonym for its actual type.

  • Structure Definition and Declaration

    • 1. After defining the structure, declare variables and assign values
#include <stdio.h>

// 定义结构体
struct Book
{
    
    
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
};

int main()
{
    
    
    // 声明变量并初始化
    struct Book book = {
    
    
        "The c programming language",
        "xxx",
        "Development Language",
        1024};

    printf("\ntitle : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n\n", book.title, book.author, book.subject, book.book_id);

    return 0;
}

Equivalent to

  • 2. When defining the structure, directly declare the variable and initialize it
#include <stdio.h>

struct Book
{
    
    
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
} book = {
    
    
    "The c programming language",
    "xxx",
    "Development Language",
    1024};
int main()
{
    
    
    printf("\ntitle : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n\n", book.title, book.author, book.subject, book.book_id);

    return 0;
}

// 另一种初始化方式
// Book book;
// strcpy(book.title, "The c programming language");
// strcpy(book.author, "xxx");
// strcpy(book.subject, "Development Language");
// book.book_id = 1024;
    1. typedefGive the type a new name.
#include <stdio.h>

// 为结构体取一个新名字
typedef struct
{
    
    
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
} Book;
int main()
{
    
    
    Book book = {
    
    
        "The c programming language",
        "xxx",
        "Development Language",
        1024};

    printf("\ntitle : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n\n", book.title, book.author, book.subject, book.book_id);

    return 0;
}
  • 4. Structure as parameter
#include <stdio.h>
#include <string.h>

// 定义结构体的同时声明变量且赋值
typedef struct
{
    
    
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
} Book;

void printBook(Book book) // 参数等价于 struct Book book
{
    
    
    book.book_id = 1234;
    printf("\ntitle : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n\n", book.title, book.author, book.subject, book.book_id);
}

int main()
{
    
    
    Book book;
    strcpy(book.title, "The c programming language");
    strcpy(book.author, "xxx");
    strcpy(book.subject, "Development Language");
    book.book_id = 1024;

    printBook(book);

    printf("\ntitle : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n\n", book.title, book.author, book.subject, book.book_id);

    return 0;
}
  • 5. Use the structure pointer to change the structure content:
void printBook(Book book) // 参数等价于 struct Book book
{
    
    
    book->book_id = 1234; // 修改
    printf("\ntitle : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n\n", book->title, book->author, book->subject, book->book_id);
}

Incoming address when using:

printBook(&book);

git_push.sh I suggest to try it out.

Guess you like

Origin blog.csdn.net/weixin_46372074/article/details/127013910