SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler offline
Enumeration Types
2
Enumeration Types
§ Example
#include <stdio.h>
int main(void)
{
int color ;
for( color = 0 ; color <= 2 ; color++ ) {
switch( color ) {
case 0 : printf( “Yellown” ) ; break ;
case 1 : printf( “Redn” ) ; break ;
case 2 : printf( “Bluen” ) ; break ;
}
}
return 0;
}
3
Enumeration Types
§ Example
#include <stdio.h>
#define YELLOW 0
#define RED 1
#define BLUE 2
int main(void)
{
int color ;
for( color = YELLOW ; color <= BLUE ; color++ ) {
switch( color ) {
case YELLOW : printf( “Yellown” ) ; break ;
case RED : printf( “Redn” ) ; break ;
case BLUE : printf( “Bluen” ) ; break ;
}
}
return 0;
}
4
Enumeration Types
§ Example
#include <stdio.h>
#define YELLOW 0
#define RED 1
#define BLUE 2
#define Color int
int main(void)
{
Color color ;
for( color = YELLOW ; color <= BLUE ; color++ ) {
switch( color ) {
case YELLOW : printf( “Yellown” ) ; break ;
case RED : printf( “Redn” ) ; break ;
case BLUE : printf( “Bluen” ) ; break ;
}
}
return 0
}
5
Enumeration Types
§ Example
#include <stdio.h>
enum color_type {Yellow, Red, Blue} ;
int main(void) {
int color ;
for( color = Yellow ; color <= Blue ; color++ ) {
switch( color ) {
case Yellow : printf( “Yellown” ) ; break ;
case Red : printf( “Redn” ) ; break ;
case Blue : printf( “Bluen” ) ; break ;
}
}
return 0;
}
6
Enumeration Types
§ Example
#include <stdio.h>
enum color_type {Yellow, Red, Blue} ;
int main(void) {
enum color_type color ;
for( color = Yellow ; color <= Blue ; color++ ) {
switch( color ) {
case Yellow : printf( “Yellown” ) ; break ;
case Red : printf( “Redn” ) ; break ;
case Blue : printf( “Bluen” ) ; break ;
}
}
return 0;
}
7
Enumeration Types
§ Example
#include <stdio.h>
int main(void) {
enum color_type {Yellow, Red, Blue} color ;
for( color = Yellow ; color <= Blue ; color++ ) {
switch( color ) {
case Yellow : printf( “Yellown” ) ; break ;
case Red : printf( “Redn” ) ; break ;
case Blue : printf( “Bluen” ) ; break ;
}
}
return 0;
}
8
Enumeration Types
§ enumeration Types
– 열거형을 선언하는데 사용
– int 형의 상수
[Ex]
enum day { sun, mon, tue, wed, thu, fri, sat };
enum day d1, d2;
d1 = fri;
tag name
첫번째 원소인 sun은 기본적으로 0의 값을 갖고,
각 열거된 순서에 따라 1,2,.. 등의 정수 값을 가진다.
변수 d1, d2를 enum day 형으로 선언.
d1, d2는 집합내의 원소만을 값으로 가짐
d1에 fri의 값을 배정하게 된다.
9
Enumeration Types
§ enumeration Types
[Ex]
enum day { sun=10, mon, tue, wed=24, thu, fri, sat=36 };
[Ex]
#define sun 10
#define mon 11
#define tue 12
#define wed 13
#define thu 24
#define fri 25
#define sat 36
10
Enumeration Types
§ enum type의 특징
– enumeration에서는 변수와 상수들을 integer type로 사용함.
[Ex]
int i;
enum { CLUBS, DIAMONDS, HEARTS, SPADES } s;
i = DIAMONDS; /* i = 1 */
s = 0; /* s = CLUBS */
s++; /* s = 1 */
i = s + 2; /* i =3 */
typedef
§ 예제
– Type 이름이 너무 길어 귀찮다.
– 좀 짧은 이름으로 바꿔 쓸 수는 없을까?
11
unsigned short var1 ;
unsigned short var2 ;
…
ushort var1 ;
ushort var2 ;
…
typedef
§ typedef
– 이전이름은 몇 단어로 구성되어도 된다.
– 그러나 새로운이름은 반드시 한 단어이어야 한다.
12
typedef 이전이름 새로운이름
typedef unsigned short ushort ;
int main()
{
ushort var1 ;
ushort var2 ;
…
}
typedef
§ 예제
13
typedef int Int ;
typedef Int INT ;
typedef int* pint ;
typedef unsigned long ulong ;
typedef unsigned long * pulong ;
int main()
{
ulong var1 ;
pulong pvar = &var1 ;
…
}
int main()
{
int var1 ;
pint pvar = &var1 ;
…
}
14
typedef
§ Example
#include <stdio.h>
#define YELLOW 0
#define RED 1
#define BLUE 2
typedef int Color ;
int main(void)
{
Color color ;
for( color = YELLOW ; color <= BLUE ; color++ ) {
switch( color ) {
case YELLOW : printf( “Yellown” ) ; break ;
case RED : printf( “Redn” ) ; break ;
case BLUE : printf( “Bluen” ) ; break ;
}
}
return 0;
}
15
typedef
§ Example
#include <stdio.h>
enum color_type {Yellow, Red, Blue} ;
typedef enum color_type Color ;
int main(void) {
Color color ;
for( color = Yellow ; color <= Blue ; color++ ) {
switch( color ) {
case Yellow : printf( “Yellown” ) ; break ;
case Red : printf( “Redn” ) ; break ;
case Blue : printf( “Bluen” ) ; break ;
}
}
return 0;
}
16
typedef
§ Example
#include <stdio.h>
typedef enum {Yellow, Red, Blue} Color ;
int main(void) {
Color color ;
for( color = Yellow ; color <= Blue ; color++ ) {
switch( color ) {
case Yellow : printf( “Yellown” ) ; break ;
case Red : printf( “Redn” ) ; break ;
case Blue : printf( “Bluen” ) ; break ;
}
}
return 0;
}
typedef
§ 예제
17
struct student {
char name[20] ;
int id ;
} ;
int main() {
struct student std1 ;
struct student std2 ;
…
}
struct student {
char name[20] ;
int id ;
} ;
typedef struct student Student ;
int main() {
Student std1 ;
Student std2 ;
…
}
typedef
§ 예제
18
typedef struct student {
char name[20] ;
int id ;
} Student;
struct student {
char name[20] ;
int id ;
} ;
typedef struct student Student ;

Mais conteúdo relacionado

Mais procurados

프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2dktm
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차Yeonah Ki
 
파이썬 함수
파이썬 함수파이썬 함수
파이썬 함수Gibson Kim
 
4. 함수포인터
4. 함수포인터4. 함수포인터
4. 함수포인터Hoyoung Jung
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차Yeonah Ki
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차Yeonah Ki
 
Key led numberbaseball ppt
Key led numberbaseball pptKey led numberbaseball ppt
Key led numberbaseball pptHeeRa Kim
 
포인터의 기초(1)
포인터의 기초(1)포인터의 기초(1)
포인터의 기초(1)Hoyoung Jung
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3guestc0587d1
 
Lua 문법 -함수
Lua 문법 -함수Lua 문법 -함수
Lua 문법 -함수Jaehoon Lee
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기Chris Ohk
 
[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handling[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handlingSeok-joon Yun
 
Swift 0x02 기본 연산자
Swift 0x02   기본 연산자Swift 0x02   기본 연산자
Swift 0x02 기본 연산자Hyun Jin Moon
 

Mais procurados (14)

프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2프로그래밍 및 실습 Chap2
프로그래밍 및 실습 Chap2
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차
 
파이썬 함수
파이썬 함수파이썬 함수
파이썬 함수
 
4. 함수포인터
4. 함수포인터4. 함수포인터
4. 함수포인터
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차
 
Key led numberbaseball ppt
Key led numberbaseball pptKey led numberbaseball ppt
Key led numberbaseball ppt
 
포인터의 기초(1)
포인터의 기초(1)포인터의 기초(1)
포인터의 기초(1)
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3
 
Lua 문법 -함수
Lua 문법 -함수Lua 문법 -함수
Lua 문법 -함수
 
Lua 문법
Lua 문법Lua 문법
Lua 문법
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
 
[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handling[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handling
 
Swift 0x02 기본 연산자
Swift 0x02   기본 연산자Swift 0x02   기본 연산자
Swift 0x02 기본 연산자
 

Destaque

6 enumerated, typedef
6 enumerated, typedef6 enumerated, typedef
6 enumerated, typedefFrijo Francis
 
Module 4 Enumeration
Module 4   EnumerationModule 4   Enumeration
Module 4 Enumerationleminhvuong
 
Test type questions
Test type questionsTest type questions
Test type questionsGerald Diana
 
Principles of Test Construction 1
Principles of Test Construction 1Principles of Test Construction 1
Principles of Test Construction 1Monica P
 

Destaque (8)

6 enumerated, typedef
6 enumerated, typedef6 enumerated, typedef
6 enumerated, typedef
 
Module 4 Enumeration
Module 4   EnumerationModule 4   Enumeration
Module 4 Enumeration
 
Essay type tests
Essay type testsEssay type tests
Essay type tests
 
Test type questions
Test type questionsTest type questions
Test type questions
 
Essays
EssaysEssays
Essays
 
8 essay test
8 essay test8 essay test
8 essay test
 
Principles of Test Construction 1
Principles of Test Construction 1Principles of Test Construction 1
Principles of Test Construction 1
 
Essay type test
Essay type testEssay type test
Essay type test
 

Mais de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 

Mais de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 

15 1. enumeration, typedef

  • 2. 2 Enumeration Types § Example #include <stdio.h> int main(void) { int color ; for( color = 0 ; color <= 2 ; color++ ) { switch( color ) { case 0 : printf( “Yellown” ) ; break ; case 1 : printf( “Redn” ) ; break ; case 2 : printf( “Bluen” ) ; break ; } } return 0; }
  • 3. 3 Enumeration Types § Example #include <stdio.h> #define YELLOW 0 #define RED 1 #define BLUE 2 int main(void) { int color ; for( color = YELLOW ; color <= BLUE ; color++ ) { switch( color ) { case YELLOW : printf( “Yellown” ) ; break ; case RED : printf( “Redn” ) ; break ; case BLUE : printf( “Bluen” ) ; break ; } } return 0; }
  • 4. 4 Enumeration Types § Example #include <stdio.h> #define YELLOW 0 #define RED 1 #define BLUE 2 #define Color int int main(void) { Color color ; for( color = YELLOW ; color <= BLUE ; color++ ) { switch( color ) { case YELLOW : printf( “Yellown” ) ; break ; case RED : printf( “Redn” ) ; break ; case BLUE : printf( “Bluen” ) ; break ; } } return 0 }
  • 5. 5 Enumeration Types § Example #include <stdio.h> enum color_type {Yellow, Red, Blue} ; int main(void) { int color ; for( color = Yellow ; color <= Blue ; color++ ) { switch( color ) { case Yellow : printf( “Yellown” ) ; break ; case Red : printf( “Redn” ) ; break ; case Blue : printf( “Bluen” ) ; break ; } } return 0; }
  • 6. 6 Enumeration Types § Example #include <stdio.h> enum color_type {Yellow, Red, Blue} ; int main(void) { enum color_type color ; for( color = Yellow ; color <= Blue ; color++ ) { switch( color ) { case Yellow : printf( “Yellown” ) ; break ; case Red : printf( “Redn” ) ; break ; case Blue : printf( “Bluen” ) ; break ; } } return 0; }
  • 7. 7 Enumeration Types § Example #include <stdio.h> int main(void) { enum color_type {Yellow, Red, Blue} color ; for( color = Yellow ; color <= Blue ; color++ ) { switch( color ) { case Yellow : printf( “Yellown” ) ; break ; case Red : printf( “Redn” ) ; break ; case Blue : printf( “Bluen” ) ; break ; } } return 0; }
  • 8. 8 Enumeration Types § enumeration Types – 열거형을 선언하는데 사용 – int 형의 상수 [Ex] enum day { sun, mon, tue, wed, thu, fri, sat }; enum day d1, d2; d1 = fri; tag name 첫번째 원소인 sun은 기본적으로 0의 값을 갖고, 각 열거된 순서에 따라 1,2,.. 등의 정수 값을 가진다. 변수 d1, d2를 enum day 형으로 선언. d1, d2는 집합내의 원소만을 값으로 가짐 d1에 fri의 값을 배정하게 된다.
  • 9. 9 Enumeration Types § enumeration Types [Ex] enum day { sun=10, mon, tue, wed=24, thu, fri, sat=36 }; [Ex] #define sun 10 #define mon 11 #define tue 12 #define wed 13 #define thu 24 #define fri 25 #define sat 36
  • 10. 10 Enumeration Types § enum type의 특징 – enumeration에서는 변수와 상수들을 integer type로 사용함. [Ex] int i; enum { CLUBS, DIAMONDS, HEARTS, SPADES } s; i = DIAMONDS; /* i = 1 */ s = 0; /* s = CLUBS */ s++; /* s = 1 */ i = s + 2; /* i =3 */
  • 11. typedef § 예제 – Type 이름이 너무 길어 귀찮다. – 좀 짧은 이름으로 바꿔 쓸 수는 없을까? 11 unsigned short var1 ; unsigned short var2 ; … ushort var1 ; ushort var2 ; …
  • 12. typedef § typedef – 이전이름은 몇 단어로 구성되어도 된다. – 그러나 새로운이름은 반드시 한 단어이어야 한다. 12 typedef 이전이름 새로운이름 typedef unsigned short ushort ; int main() { ushort var1 ; ushort var2 ; … }
  • 13. typedef § 예제 13 typedef int Int ; typedef Int INT ; typedef int* pint ; typedef unsigned long ulong ; typedef unsigned long * pulong ; int main() { ulong var1 ; pulong pvar = &var1 ; … } int main() { int var1 ; pint pvar = &var1 ; … }
  • 14. 14 typedef § Example #include <stdio.h> #define YELLOW 0 #define RED 1 #define BLUE 2 typedef int Color ; int main(void) { Color color ; for( color = YELLOW ; color <= BLUE ; color++ ) { switch( color ) { case YELLOW : printf( “Yellown” ) ; break ; case RED : printf( “Redn” ) ; break ; case BLUE : printf( “Bluen” ) ; break ; } } return 0; }
  • 15. 15 typedef § Example #include <stdio.h> enum color_type {Yellow, Red, Blue} ; typedef enum color_type Color ; int main(void) { Color color ; for( color = Yellow ; color <= Blue ; color++ ) { switch( color ) { case Yellow : printf( “Yellown” ) ; break ; case Red : printf( “Redn” ) ; break ; case Blue : printf( “Bluen” ) ; break ; } } return 0; }
  • 16. 16 typedef § Example #include <stdio.h> typedef enum {Yellow, Red, Blue} Color ; int main(void) { Color color ; for( color = Yellow ; color <= Blue ; color++ ) { switch( color ) { case Yellow : printf( “Yellown” ) ; break ; case Red : printf( “Redn” ) ; break ; case Blue : printf( “Bluen” ) ; break ; } } return 0; }
  • 17. typedef § 예제 17 struct student { char name[20] ; int id ; } ; int main() { struct student std1 ; struct student std2 ; … } struct student { char name[20] ; int id ; } ; typedef struct student Student ; int main() { Student std1 ; Student std2 ; … }
  • 18. typedef § 예제 18 typedef struct student { char name[20] ; int id ; } Student; struct student { char name[20] ; int id ; } ; typedef struct student Student ;