What is the Identifier After the Struct in C Language?

What is the Identifier After the Struct in C Language?Question:When reading C language code (or framework code), you may encounter the following syntax in the struct part:

struct student{    char name[20];    int age;    int grade;}xiaoMing;

What is the identifier xiaoMing in this case?Answer:This is an instance of the struct student. With this syntax, you can directly use the variable xiaoMing in subsequent programming development, without needing to declare it in the conventional way as struct student xiaoMing. For example, in the following code:

#include <stdio.h>
#include <string.h>
struct student{    char name[20];    int age;    int grade;}xiaoMing, xiaoQiang;
int main(){    strcpy(xiaoMing.name, "Xiao Ming");    xiaoMing.age = 18;    xiaoMing.grade = 100;    struct student xiaoHong = {"Xiao Hong", 19, 100};    printf("Name: %s, Age: %d, Grade: %d\n", xiaoMing.name, xiaoMing.age, xiaoMing.grade);    printf("Name: %s, Age: %d, Grade: %d\n", xiaoHong.name, xiaoHong.age, xiaoHong.grade);    return 0;}

The code compiles and runs, producing the output:

Name: Xiao Ming, Age: 18, Grade: 100
Name: Xiao Hong, Age: 19, Grade: 100

Disclaimer: The content is for reference only and does not guarantee accuracy!

Leave a Comment