#include <stdio.h>
int main() {
/* 구조체 정의
struct 구조체이름 {
자료형1 변수이름1;
자료형2 변수이름2;
};
*/
struct Fruit {
char *name;
int price;
float weight;
};
struct Fruit apple = {"사과", 1500, 0.25};
struct Fruit banana = {"바나나", 500, 0.12};
printf("%s / %d원 / %.2fkg\n", apple.name, apple.price, apple.weight);
// 구조체 배열 (자료형 배열이름[배열크기])
// struct 구초제이름 배열이름[배열크기]
struct Fruit fruits[3] = {
{"딸기", 6000, 0.5},
{"수박", 22000, 7.1},
{"귤", 4000, 1.0}
};
printf("%s\n", fruits[0].name);
// 수박의 가격 출력
printf("%d원\n", fruits[1].price);
// 귤의 이름, 무게 출력
printf("%s / %.1fkg\n", fruits[2].name, fruits[2].weight);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: