BIG

[ C ] Sample Code - 구조체-02 - 구조체를 배열로 선언하여 사용하기

 

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

struct Student {
    char name[20];
    int age;
    char major[20];
};

int main() {
    struct Student student[3];
    int i;

    for (i = 0; i < 3; i++) {
        printf("Enter student %d's name: ", i+1);
        scanf("%s", student[i].name);
        printf("Enter student %d's age: ", i+1);
        scanf("%d", &student[i].age);
        printf("Enter student %d's major: ", i+1);
        scanf("%s", student[i].major);
    }

    for (i = 0; i < 3; i++) {
        printf("Student %d's name: %s\n", i+1, student[i].name);
        printf("Student %d's age: %d\n", i+1, student[i].age);
        printf("Student %d's major: %s\n", i+1, student[i].major);
    }

    return 0;
}

 

출력결과

LIST