Successfully reported this slideshow.
Upcoming SlideShare
×

15 1. enumeration, typedef

646 views

Published on

Published in: Technology, Art & Photos
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

15 1. enumeration, typedef

1. 1. Enumeration Types
2. 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. 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. 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. 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. 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. 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. 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. 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. 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. 11. typedef § 예제 – Type 이름이 너무 길어 귀찮다. – 좀 짧은 이름으로 바꿔 쓸 수는 없을까? 11 unsigned short var1 ; unsigned short var2 ; … ushort var1 ; ushort var2 ; …
12. 12. typedef § typedef – 이전이름은 몇 단어로 구성되어도 된다. – 그러나 새로운이름은 반드시 한 단어이어야 한다. 12 typedef 이전이름 새로운이름 typedef unsigned short ushort ; int main() { ushort var1 ; ushort var2 ; … }
13. 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. 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. 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. 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. 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. 18. typedef § 예제 18 typedef struct student { char name[20] ; int id ; } Student; struct student { char name[20] ; int id ; } ; typedef struct student Student ;