12 include stdio h int mainvoid int num

  • Slides: 77
Download presentation
12장. 고급 기능 #include <stdio. h> int main(void) { int num; printf(“Please enter an

12장. 고급 기능 #include <stdio. h> int main(void) { int num; printf(“Please enter an integer: "); scanf("%d", &num); if ( num < 0 ) printf("Is negative. n"); printf("num = %dn", num); return 0; } 1

전처리기 §매크로 함수를 정의하고 사용하는 예(1/3) 01: 02: 03: 04: 05: 06: 07: 08:

전처리기 §매크로 함수를 정의하고 사용하는 예(1/3) 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /* Ex 12_01. c */ #include <stdio. h> #define SQUARE(n) n*n 매크로 함수의 정의 int square(int n) { return n*n; } int main(void) { int result = SQUARE(3); printf("3의 제곱 : %dn", result); 매크로 함수의 호출 result = SQUARE(1+2); 매크로 함수의 호출 printf("SQUARE(1+2) = %dn", result); 12장. 고급 기능 11

전처리기 §매크로 # 연산자와 ## 연산자 사용 예(1/2) 01: 02: 03: 04: 05: 06:

전처리기 §매크로 # 연산자와 ## 연산자 사용 예(1/2) 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: /* Ex 12_02. c */ #include <stdio. h> #define PRINT 1(x) printf("x = %dn", x) #define PRINT 2(x) printf(#x" = %dn", x) #define MAKE_FUNC(name) void fn##name(void) { printf("fn"#name" 호출n"); } 매크로 함수의 정의 #을 이용한 매크로 함수의 정의 #과 ##을 이용한 매크로 함수의 정의 MAKE_FUNC(test 1, 1) MAKE_FUNC(test 2, 2) int main(void) { int num = 10; PRINT 1(num); PRINT 2(num); 12장. 고급 기능 18

전처리기 §조건부 컴파일 #ifdef를 이용한 디버깅 정보 출력(1/2) 01: 02: 03: 04: 05: 06:

전처리기 §조건부 컴파일 #ifdef를 이용한 디버깅 정보 출력(1/2) 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /* Ex 12_03. c */ #include <stdio. h> #define DEBUG 매크로 심볼의 정의 int Get. Factorial(int n); int main(void) { int result; result = Get. Factorial(5); printf("result = %dn", result); } return 0; 12장. 고급 기능 27

전처리기 §조건부 컴파일 #ifdef를 이용한 디버깅 정보 출력(2/2) 19: 20: 21: 22: 23: 24:

전처리기 §조건부 컴파일 #ifdef를 이용한 디버깅 정보 출력(2/2) 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: int Get. Factorial(int n) { #ifdef DEBUG printf("Get. Factorial 함수 호출 : "); printf("n = %dn", n); #endif } 조건부 컴파일 if( n <= 1 ) return 1; return n * Get. Factorial(n - 1); 12장. 고급 기능 28

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(1/5) Array. h

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(1/5) Array. h 01: 02: 03: 04: /* Array. h */ void Print. Array(const int *arr, int size); int Get. Sum. Of. Array(int *arr, int size); void Sort. Array(int *arr, int size); 함수의 선언 Array. c 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: /* Array. c */ #include <stdio. h> #include "Array. h" 라이브러리 헤더 파일 포함 자기 자신의 헤더 파일 포함 void Print. Array(const int *arr, int size) { int i; //arr[0] = 100; for(i = 0 ; i < size ; i++) printf("%d ", arr[i]); 12장. 고급 기능 42

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(2/5) Array. c(continued)

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(2/5) Array. c(continued) 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: } printf("n"); int Get. Sum. Of. Array(int *arr, int size) { int i; int total; for(i = 0, total = 0 ; i < size ; i++) total += arr[i]; } return total; void Sort. Array(int *arr, int size) { int i, j, index; int temp; 12장. 고급 기능 43

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(3/5) Array. c(continued)

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(3/5) Array. c(continued) 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: } for(i = 0; i < size-1 ; i++) { index = i; for(j = i+1 ; j < size ; j++) { if( arr[index] > arr[j] ) index = j; } temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; } 12장. 고급 기능 44

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(4/5) Main. c

분할 컴파일 §헤더 파일과 소스 파일 다른 파일에 정의된 함수의 호출 예(4/5) Main. c 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: /* Main. c */ #include <stdio. h> #include "Array. h" 라이브러리 헤더 파일 포함 사용자 정의 헤더 파일 포함 int main(void) { int x[5] = {43, 6, 24, 88, 34}; int y[10] = {12, 35, 7, 45, 78, 22, 98, 77, 1, 28}; printf("x 배열 : "); Print. Array(x, 5); printf("합계 : %dnn", Get. Sum. Of. Array(x, 5)); printf("y 배열 : "); Print. Array(y, 10); Sort. Array(y, 10); 12장. 고급 기능 45

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(1/5) Pont.

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(1/5) Pont. h 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: /* Point. h */ struct point { int x; int y; }; typedef struct point POINT; 구조체의 정의 구조체에 대한 typedef 정의 void Print. Point(const POINT *p); void Move. Point(POINT *p, int dx, int dy); double Get. Distance(const POINT *p 1, const POINT *p 2); 12장. 고급 기능 구조체를 매개변수로 갖는 함수의 선언 49

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(2/5) Pont.

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(2/5) Pont. c 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: /* Point. c */ #include <stdio. h> #include <math. h> #include "Point. h" 라이브러리 헤더 파일 포함 사용자 정의 헤더 파일 포함 void Print. Point(const POINT *p) { printf("(%d, %d)n", p->x, p->y); } void Move. Point(POINT *p, int dx, int dy) { p->x += dx; p->y += dy; } 12장. 고급 기능 50

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(3/5) Pont.

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(3/5) Pont. c(continued) 16: double Get. Distance(const POINT *p 1, const POINT *p 2) 17: { 18: int dx = p 2 ->x - p 1 ->x; 19: int dy = p 2 ->y - p 1 ->y; 20: return sqrt(dx*dx+dy*dy); 21: } Main. c 01: 02: 03: 04: 05: 06: 07: 08: 09: /* Main. c */ #include <stdio. h> #include "Point. h" 라이브러리 헤더 파일 포함 사용자 정의 헤더 파일 포함 int main(void) { POINT p 1 = {0, 0}; POINT p 2 = {300, 400}; 12장. 고급 기능 51

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(3/5) Main.

분할 컴파일 §헤더 파일과 소스 파일 여러 소스 파일에 사용되는 구조체의 정의 예(3/5) Main. c(continued) 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: } printf("p 1 = "); Print. Point(&p 1); printf("p 2 = "); Print. Point(&p 2); printf("두 점 사이의 거리 : %fn", Get. Distance(&p 1, &p 2)); Move. Point(&p 2, 10); printf("Move. Point 호출 후 p 2 = "); Print. Point(&p 2); return 0; 12장. 고급 기능 52

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(1/5)

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(1/5) Pont. h 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: /* Point. h */ struct point { int x; int y; }; typedef struct point POINT; void Print. Point(const POINT *p); void Move. Point(POINT *p, int dx, int dy); double Get. Distance(const POINT *p 1, const POINT *p 2); void Set. Origin(int x, int y); extern POINT g_origin; 전역 변수의 extern 선언 12장. 고급 기능 56

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(2/5)

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(2/5) Pont. c 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: /* Point. c */ #include <stdio. h> #include <math. h> #include "Point. h" POINT g_origin; 전역 변수 선언 void Print. Point(const POINT *p) { printf("(%d, %d)n", p->x, p->y); } void Move. Point(POINT *p, int dx, int dy) { p->x += dx; p->y += dy; } 12장. 고급 기능 57

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(3/5)

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(3/5) Pont. c(continued) 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: double Get. Distance(const POINT *p 1, const POINT *p 2) { int dx = p 2 ->x - p 1 ->x; int dy = p 2 ->y - p 1 ->y; return sqrt(dx*dx+dy*dy); } void Set. Origin(int x, int y) { g_origin. x = x; g_origin. y = y; } 전역 변수의 사용 12장. 고급 기능 58

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(4/5)

분할 컴파일 §헤더 파일과 소스 파일 다른 소스 파일에 정의된 전역 변수의 사용 예(4/5) Main. c 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: /* Main. c */ #include <stdio. h> #include "Point. h" 사용자 정의 헤더 파일 포함 int main(void) { POINT p 1 = {100, 200}; POINT p 2 = {300, 400}; printf("p 1 = "); Print. Point(&p 1); printf("p 2 = "); Print. Point(&p 2); printf("두 점 사이의 거리 : %fn", Get. Distance(&p 1, &p 2)); 12장. 고급 기능 59

분할 컴파일 §헤더 파일의 중복 문제 헤더 파일의 중복 포함 막기(1/5) Pont. h 01:

분할 컴파일 §헤더 파일의 중복 문제 헤더 파일의 중복 포함 막기(1/5) Pont. h 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /* Point. h */ #ifndef POINT_H #define POINT_H 헤더 파일의 중복 포함 막기 struct point { int x; int y; }; typedef struct point POINT; void Print. Point(const POINT *p); void Move. Point(POINT *p, int dx, int dy); double Get. Distance(const POINT *p 1, const POINT *p 2); void Set. Origin(int x, int y); extern POINT g_origin; #endif 헤더 파일의 중복 포함 막기 12장. 고급 기능 64

분할 컴파일 §헤더 파일의 중복 문제 헤더 파일의 중복 포함 막기(2/5) Pont. c 01:

분할 컴파일 §헤더 파일의 중복 문제 헤더 파일의 중복 포함 막기(2/5) Pont. c 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: /* Point. c */ #include <stdio. h> #include <math. h> #include "Point. h" POINT g_origin; void Print. Point(const POINT *p) { printf("(%d, %d)n", p->x, p->y); } void Move. Point(POINT *p, int dx, int dy) { p->x += dx; p->y += dy; } 12장. 고급 기능 65

분할 컴파일 §헤더 파일의 중복 문제 헤더 파일의 중복 포함 막기(3/5) Pont. c(continued) 18:

분할 컴파일 §헤더 파일의 중복 문제 헤더 파일의 중복 포함 막기(3/5) Pont. c(continued) 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: double Get. Distance(const POINT *p 1, const POINT *p 2) { int dx = p 2 ->x - p 1 ->x; int dy = p 2 ->y - p 1 ->y; return sqrt(dx*dx+dy*dy); } void Set. Origin(int x, int y) { g_origin. x = x; g_origin. y = y; } 12장. 고급 기능 66

main 함수의 매개변수 §main 함수의 매개변수 명령행 인자를 이용한 계산기 프로그램(1/2) 01: 02: 03:

main 함수의 매개변수 §main 함수의 매개변수 명령행 인자를 이용한 계산기 프로그램(1/2) 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: /* Ex 12_09. c */ #include <stdio. h> #include <stdlib. h> int main(int argc, char* argv[ ]) { int lhs, rhs; char op; 매개변수를 갖는 main 함수 if( argc < 4 ) { printf("Usage : Calc value 1 op value 2n"); return -1; } lhs = atoi(argv[1]); op = argv[2][0]; rhs = atoi(argv[3]); 명령행 인자의 개수 확인 명령행 인자 사용 12장. 고급 기능 71

main 함수의 매개변수 §main 함수의 매개변수 명령행 인자를 이용한 계산기 프로그램(2/2) 20: 21: 22:

main 함수의 매개변수 §main 함수의 매개변수 명령행 인자를 이용한 계산기 프로그램(2/2) 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: } switch( op ) { case '+': printf("%d + %d = %dn", lhs, rhs, lhs+rhs); break; case '-': printf("%d - %d = %dn", lhs, rhs, lhs-rhs); break; case '*': printf("%d * %d = %dn", lhs, rhs, lhs*rhs); break; case '/': printf("%d / %d = %dn", lhs, rhs, lhs/rhs); break; default : printf("잘못 입력하셨습니다. n"); break; } 실행시 명령 프롬프트에서 “Calc. exe 10 + 20” 이라고 지정한다. return 0; 12장. 고급 기능 72