Language/C
[따배씨] 14.18 열거형
migrationArc
2021. 7. 6. 23:47
따배씨 - 따라하며 배우는 C언어
14강 구조체_2
14.18 열거형 Enumerated Types
- 열거형: 정수형 상수가 마치 이름이 있는 것 처럼 사용 할 수 있게 도와줌
#include <stdio.h>
/*
int c = 0; // red: 0, orange: 1, yellow:2, green:3, ..
if (c == 2)
printf("yellow");
else if (c == 1)
printf("orange");
...
*/
/*
#define RED 1
#define ORANGE 2
#define YELLOW 3
int c = YELLOW;
if (c == YELLOW)
printf("yellow");
else if (c == ORANGE)
printf("orange");
...
*/
int main(){
/*
Enumerated type
- Symbolic names to represent integer constants
- Improve readability and make it easy to maintain
- enum-specifier (struct-specifier, union-specifier)
Enumerators
- The symolic constants
*/
enum spectrum { red, orange, yellow, green, blue, violet };
// 0 1 2 3 4 5
// 나열되어 있는 정수들에게 이름을 붙여 줌
enum spectrum color;
color = blue;
if (color == yellow)
printf("yellow"); //Note: enumerators are not strings
for (color = red; color <= violet; color++) //Note: ++ operator doesn't allow in C++, use type int.
printf("%d\n", color);
printf("red = %d, orange = %d\n", red, orange);
enum kids { jackjack, dash, snoopy, nano, pitz };
// nina has a value of 3
enum kids my_kids = nano;
printf("nano %d %d\n", my_kids, nano);
return 0;
}
#include <stdio.h>
int main(){
enum levels {low = 100, medium = 500, high = 2000};
int score = 800;
if (score > high)
printf("High score!\n");
else if (score > medium)
printf("Good job\n");
// Good job
else if (score > low)
printf("Not bad\n");
else
printf("Do your best\n");
enum pet { cat, dog = 10, lion, tiger };
// puma has a value of 11
printf("Cat %d\n", cat);
// Cat 0
printf("Lion %d\n", lion);
// Lion 11
return 0;
}
enum pet { cat, dog = 10, lion, tiger };
- 숫자를 지정 할 수 있고, 지정된 값을 이어서 증가하여 지정 됨
이 글의 모든 사진과 내용의 출처는 홍정모 교수님께 있음을 알려드립니다.
http://blog.naver.com/atelierjpro
실리콘 밸리의 프로그래머 : 네이버 블로그
안녕하세요! 홍정모 블로그에 오신 것을 환영합니다. 주로 프로그래밍 관련 메모 용도로 사용합니다. 강의 수강하시는 분들은 홍정모 연구소 카페로 오세요.
blog.naver.com
http://www.inflearn.com/course/following-c
홍정모의 따라하며 배우는 C언어 - 인프런 | 강의
'따배씨++'의 성원에 힘입어 새롭게 개발된 C 언어로 시작하는 프로그래밍 입문 강의입니다. '따배씨'와 함께 프로그래밍 인생을 업그레이드 해보세요., 따라하며 배우는 C언어 '따배씨++'의 성원
www.inflearn.com