typedef

개념

  • 어떤 자료형에 타입 별칭(type alias)을 부여하는 키워드
  • typedef를 이용해 구조체 등의 자료형을 별칭으로 간결하게 표현할 수 있다.

문법

  • 아래는 unsigned int 자료형을 UINT 라고 별칭을 붙이는 경우이다.
1
typedef unsigned int UINT;
  • 구조체에 적용해보기
1
2
3
4
5
6
typedef struct {
  float x, y;
  float radius;
} Circle;

Circle c1;

typedef 사용 여부 비교

  • 미사용시
1
2
3
4
5
6
struct Circle {
  float x, y;
  float radius;
}

struct Circle c1, c2, ...; // struct Circle 이라고 붙여야 함  
  • 사용시
1
2
3
4
5
6
typedef struct {
  float x, y;
  float radius;
} Circle;

Circle c1, c2, ...;

Reference

C 프로그래밍 (김형근, 곽덕훈, 정재화 공저)
C 프로그래밍 강의 (방송통신대 - 이병래)

Comments