define define define PRINT PI TWOPI MAXINT EOF

  • Slides: 29
Download presentation

단순 매크로의 예 #define #define #define PRINT PI TWOPI MAX_INT EOF MAX_STUDENTS EPS DIGITS

단순 매크로의 예 #define #define #define PRINT PI TWOPI MAX_INT EOF MAX_STUDENTS EPS DIGITS BRACKET getchar() putchar() 214748364 7보다는 MAX_INT 가 낫죠 printf 3. 141592 (3. 141592 * 2. 0) 2147483647 (-1) 2000 1. 0 e-9 "0123456789" "(){}[]" getc(stdin) putc(stdout) // 원주율의 2배 // 최대 정수 // 파일의 끝 표시 // 최대 학생 수 // 실수의 계산 한계 // 문자 상수 정의 // stdio. h에 정의 사람은 숫자 보다 기호를 잘 기억합니 다. Slide 6 (of 29)

예제 #1 #include <stdio. h> #define AND #define OR #define NOT #define ISNOT &&

예제 #1 #include <stdio. h> #define AND #define OR #define NOT #define ISNOT && || ! == != int search(int list[], int n, int key) { int i = 0; C프로그램 을 다른 언 어처럼 작성 할 수 있습 니다. while( i < n AND list[i] != key ) i++; if( i IS n ) return -1; else return i; } int main(void) { int m[] = { 1, 2, 3, 4, 5, 6, 7 }; } printf("%dn", search(m, sizeof(m)/sizeof(m[0]), 5)); return 0; Slide 7 (of 29)

헤더 파일 이중 포함 방지 /*** *stdio. h - definitions/declarations for standard I/O routines

헤더 파일 이중 포함 방지 /*** *stdio. h - definitions/declarations for standard I/O routines ****/ #ifndef _INC_STDIO #define _INC_STDIO. . . . 헤더 파일이 포함되면 매크 #endif 로가 정의되어 서 이중 포함 을 방지합니다. Slide 15 (of 29)

#undef · #undef: 매크로의 정의를 취소 1. 2. #include <stdio. h> #define DEBUG 3.

#undef · #undef: 매크로의 정의를 취소 1. 2. #include <stdio. h> #define DEBUG 3. 4. 5. 6. 7. int main(void) { #ifdef DEBUG printf("DEBUG이 정의되었습니다. n"); #endif 8. #undef DEBUG 9. #ifdef DEBUG 10. printf("DEBUG이 정의되었습니다. n"); 11. #endif 12. return 0; 13. } // DEBUG 매크로의 정의를 취소 // 컴파일되지 않는다. Slide 16 (of 29)

예제 #1 power. h multiple_source. c // 다중 소스 파일 #include <stdio. h> #include

예제 #1 power. h multiple_source. c // 다중 소스 파일 #include <stdio. h> #include "power. h" int main(void) { int x, y; printf("x의 값을 입력하시오: "); scanf("%d", &x); printf("y의 값을 입력하시오: "); scanf("%d", &y); printf("%d의 %d 제곱값은 %fn", x, y, power(x, y)); } return 0; // power. c에 대한 헤더 파일 #ifndef POWER_H #define POWER_H double power(int x, int y); #endif power. c // 다중 소스 파일 #include "power. h" double power(int x, int y) { double result = 1. 0; int i; for(i = 0; i < y; i++) result *= x; } return result; Slide 19 (of 29)

bit_AND. c 1. 2. // 비트 단위 AND #include <stdio. h> 3. 4. 5.

bit_AND. c 1. 2. // 비트 단위 AND #include <stdio. h> 3. 4. 5. 6. 7. int main(void) { int x = 13; int y = 15; int z = x & y; 8. printf("%08 X n", z); 9. 10. } return 0; // 00000000 00001101 // 00000000 00001110 // 00000000 00001100 0000000 D Slide 21 (of 29)

bit_OR. c 1. 2. // 비트 단위 OR #include <stdio. h> 3. 4. 5.

bit_OR. c 1. 2. // 비트 단위 OR #include <stdio. h> 3. 4. 5. 6. 7. int main(void) { int x = 9; // 00000000 00001001 int y = 10; // 00000000 00001010 int z = x | y; // 00000000 00001011 8. printf("%08 X n", z); 9. 10. } return 0; 0000000 B Slide 22 (of 29)

bit_NOT. c 1. 2. // 비트 단위 NOT #include <stdio. h> 3. 4. 5.

bit_NOT. c 1. 2. // 비트 단위 NOT #include <stdio. h> 3. 4. 5. 6. int main(void) { int x = 9; int z = ~x; // 00000000 00001001 // 11111111 11110110 7. printf("%08 X (%d)n", z, z); 8. 9. return 0; } FFFFFFF 6 (-10) Slide 23 (of 29)

bit_field. c 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

bit_field. c 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. // 비트 필드 구조체 #include <stdio. h> struct product { unsigned style : 3; unsigned size : 2; unsigned color : 1; }; struct product p 1; int main(void) { p 1. style = 5; p 1. size = 3; p 1. color = 1; printf("style=%d size=%d color=%dn", p 1. style, p 1. size, p 1. color); printf("sizeof(p 1)=%dn", sizeof(p 1)); printf("p 1=%xn", p 1); } return 0; style=5 size=3 color=1 sizeof(p 1)=4 p 1=3 d Slide 27 (of 29)

Q&A Slide 29 (of 29)

Q&A Slide 29 (of 29)