How to use structures in C programming to output student information?

We can define a student’s information using a struct, and then use the printf function to output the student’s information.

First, we need to define a structure for a student, which includes information such as the student’s name, age, and grades.

struct Student {
    char name[100];
    int age;
    float score;
};

Next, we can create a structure variable for a student and assign values to it:

struct Student student1;
strcpy(student1.name, "张三");
student1.age = 18;
student1.score = 90.5;

Finally, we can use the printf function to display the students’ information.

printf("姓名:%s\n", student1.name);
printf("年龄:%d\n", student1.age);
printf("成绩:%.2f\n", student1.score);

The complete code is as follows:

#include <stdio.h>
#include <string.h>

struct Student {
    char name[100];
    int age;
    float score;
};

int main() {
    struct Student student1;
    strcpy(student1.name, "张三");
    student1.age = 18;
    student1.score = 90.5;

    printf("姓名:%s\n", student1.name);
    printf("年龄:%d\n", student1.age);
    printf("成绩:%.2f\n", student1.score);
    
    return 0;
}

The output of this will be:

姓名:张三
年龄:18
成绩:90.50
bannerAds