Day 6: C의 Structs: 클래스를 사용하지 않은 데이터 조직

발행: (2026년 1월 14일 오전 12:45 GMT+9)
2 min read
원문: Dev.to

Source: Dev.to

소개

Java나 Python 같은 언어에서는 클래스를 사용해 데이터를 하나로 묶습니다.
C에는 클래스가 없고, 더 원시적인 것이 있습니다: struct(구조체)입니다.

struct (구조체)는 사용자 정의 데이터 타입을 만드는 방법입니다. 구조체가 없으면 학생의 데이터를 저장하려면 각각 별도의 변수를 선언해야 합니다:

char *name1;
int   age1;
float gpa1;

구조체를 사용하면 이러한 필드들을 하나의 Student 타입으로 묶을 수 있으며, 이는 객체‑지향 프로그래밍에서 “객체” 개념의 선구자 역할을 합니다.

예시: 구조체 정의 및 사용

// Day 6: Grouping chaos into order

#include 
#include 

// Before: Messy variables scattered everywhere
// char *name1 = "Alice"; int age1 = 20;

// After: A custom data type
// We define a blueprint called "Student"
typedef struct {
    char  name[50];
    int   age;
    float gpa;
} Student;

int main() {
    // Treat the data as a single "object"
    // Initialize it like an array
    Student s1 = {"Alice", 20, 3.8};

    // Access fields using the dot operator (.)
    printf("Name: %s\n", s1.name);
    printf("GPA:  %.2f\n", s1.gpa);

    return 0;
}

GitHub에서 소스 코드를 확인하세요:

Back to Blog

관련 글

더 보기 »