C Express 9 2012 All rights reserved ress

  • Slides: 41
Download presentation
쉽게 풀어쓴 C언어 Express 제 9장 함수와 변수 © 2012 생능출판사 All rights reserved

쉽게 풀어쓴 C언어 Express 제 9장 함수와 변수 © 2012 생능출판사 All rights reserved ress p C Ex

지역 변수의 범위 void sub 1(void) { { int y; . . . }

지역 변수의 범위 void sub 1(void) { { int y; . . . } y = 4; } © 2012 생능출판사 All rights reserved

지역 변수 예제 #include <stdio. h> int main(void) { int i; 2 21 1

지역 변수 예제 #include <stdio. h> int main(void) { int i; 2 21 1 temp for (i = 0; i < 5; i++) { int temp = 1; printf("temp = %dn", temp); temp++; } return 0; } © 2012 생능출판사 All rights reserved 블록이 시작할 때 마다 생성되어 초기화된다. temp = 1 temp = 1

지역 변수의 초깃값 #include <stdio. h> int main(void) { int temp; printf("temp = %dn",

지역 변수의 초깃값 #include <stdio. h> int main(void) { int temp; printf("temp = %dn", temp); return 0; } 초기화 되지 않았으 므로 쓰레기 값을 가진다. temp = -858993460 © 2012 생능출판사 All rights reserved

함수의 매개 변수 int inc (int counter) { counter++; return counter; } © 2012

함수의 매개 변수 int inc (int counter) { counter++; return counter; } © 2012 생능출판사 All rights reserved 매개 변수도 일종의 지역 변수

함수의 매개 변수 #include <stdio. h> int inc(int counter); int main(void) { int i;

함수의 매개 변수 #include <stdio. h> int inc(int counter); int main(void) { int i; 10 10 i 11 counter i = 10; printf("함수 호출 전 i = %dn", i); inc(i); printf("함수 호출 후 i = %dn", i); return 0; } © 2012 생능출판사 All rights reserved int inc(int counter) { counter++; return counter; } 함수 호출 전 i = 10 함수 호출 후 i = 10

전역 변수의 초깃값과 생존 시간 #include <stdio. h> int counter; // 전역 변수 *전역변수의

전역 변수의 초깃값과 생존 시간 #include <stdio. h> int counter; // 전역 변수 *전역변수의 초깃값 : 0 *생존기간 : 프로그램 시작부터 종료 void set_counter(int i) { counter = i; } int main(void) { printf("counter = %dn", counter); counter = 100; printf("counter = %dn", counter); set_counter(20); printf("counter = %dn", counter); return 0; } © 2012 생능출판사 All rights reserved 20 i 0 20 100 r counter = 0 counter = 100 counter = 20

저장 유형 지정자: static #include <stdio. h> void sub(void); int main(void) { int i;

저장 유형 지정자: static #include <stdio. h> void sub(void); int main(void) { int i; for (i = 0; i < 3; i++) sub(); return 0; } 자동 지역 변수 정적 지역 변수로써 static을 붙이면 지역 변수가 정적 변수가 된다. void sub(void) { int auto_count = 0; static int static_count = 0; auto_count++; static_count++; printf("auto_count = %dn", auto_count); printf("static_count = %dn", static_count); } © 2012 생능출판사 All rights reserved auto_count = 1 static_count = 1 auto_count = 1 static_count = 2 auto_count = 1 static_count = 3

로그인 횟수 제한 예제 #include <stdio. h> #include <stdlib. h> #define SUCCESS 1 #define

로그인 횟수 제한 예제 #include <stdio. h> #include <stdlib. h> #define SUCCESS 1 #define FAIL 2 #define LIMIT 3 int check(int id, int password); int main(void) { int id, password; while (1) { printf("id: ____bb"); scanf("%d", &id); printf("password: ____bb"); scanf("%d", &password); if (check(id, password) == SUCCESS) break; } printf("로그인 성공n"); return 0; } © 2012 생능출판사 All rights reserved int check(int id, int password) { static int super_id = 1234; static int super_password = 5678; static int try_count = 0; 정적 지역 변수 if (++try_count >= LIMIT) { printf("횟수 초과n"); exit(1); } if (id == super_id && password == super_password) return SUCCESS; id: 1111 password: 1111 else id: 1111 return FAIL; password : 1111 id: 1111 } password : 1111 횟수 초과

연결 예제 linkage 1. c #include <stdio. h> int all_files; // 다른 소스 파일에서도

연결 예제 linkage 1. c #include <stdio. h> int all_files; // 다른 소스 파일에서도 사용할 수 있는 전역 변수 static int this_file; // 현재의 소스 파일에서만 사용할 수 있는 외부 변수 void sub(); int main(void) { 연결 sub(); printf("%dn", all_files); return 0; } linkage 2. c extern int all_files; void sub(void) { all_files = 10; } © 2012 생능출판사 All rights reserved this_file 10 0 all_files 10 0

정적 함수(static function) main. c #include <stdio. h> void f 2(); int main(void) {

정적 함수(static function) main. c #include <stdio. h> void f 2(); int main(void) { f 2(); return 0; } sub. c static이 붙은 함수는 해당 소스 파일 안에서만 사용할 수 있다. static void f 1() { printf("f 1()이 호출되었습니다. n"); } void f 2() { f 1(); printf("f 2()가 호출되었습니다. n"); } © 2012 생능출판사 All rights reserved

다중 소스 파일 예제: 난수 발생 main. c random. c #include <stdio. h> unsigned

다중 소스 파일 예제: 난수 발생 main. c random. c #include <stdio. h> unsigned random(void); extern unsigned call_count; 조 int main(void) { register int i; // 외부 변수 참 // 레지스터 변수 // 난수 발생 함수 #define SEED 17 #define MULT 25173 #define INC 13849 #define MOD 65536 unsigned call_count = 0; // 전역 변수 for (i = 0; i < 10; i++) printf("%d ", random()); printf("n"); printf("함수가 호출된 횟수 = %dn", call_count); return 0; } © 2012 생능출판사 All rights reserved 48574 61999 40372 31453 39802 35227 15504 29161 14966 52039 함수가 호출된 횟수 = 10

순환/재귀(recursion)이란? · 알고리즘이나 함수가 수행 도중에 자기 자신을 다시 호출하여 문제를 해결하는 기법 ·

순환/재귀(recursion)이란? · 알고리즘이나 함수가 수행 도중에 자기 자신을 다시 호출하여 문제를 해결하는 기법 · 팩토리얼의 정의 int factorial(int n) { if (n <= 1) return 1; else return n * factorial(n-1); } © 2012 생능출판사 All rights reserved factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) =3*2*1 =3*2 =6

피보나치 수열(Fibonacci series) · · 순환 호출을 사용하면 비효율적인 예 피보나치 수열: 0, 1,

피보나치 수열(Fibonacci series) · · 순환 호출을 사용하면 비효율적인 예 피보나치 수열: 0, 1, 1, 2, 3, 5, 8, 13, 21, … · 순환 사용 구현 unsigned fib(unsigned n) { if (n < 2) return n; return fib(n - 2) + fib(n - 1); } © 2012 생능출판사 All rights reserved · 반복 사용 구현 unsigned fib(unsigned n) { int a = 0, b = 1, c; if (n < 2) return n; while (1) { c = a + b; if (--n < 2) return c; a = b; b = c; } }

하노이 타워 예제 #include <stdio. h> // n개의 원판을 임시 막대 tmp를 사용하여 from에서

하노이 타워 예제 #include <stdio. h> // n개의 원판을 임시 막대 tmp를 사용하여 from에서 to로 이동 void hanoi_tower(int n, char from, char tmp, char to) { if (n == 1) printf("원판 1: %c => %cn", from, to); else { hanoi_tower(n - 1, from, to, tmp); printf("원판 %d: %c => %cn", n, from, to); hanoi_tower(n - 1, tmp, from, to); } } int main(void) { hanoi_tower(4, 'A', 'B', 'C'); return 0; } © 2012 생능출판사 All rights reserved 원판 원판 원판 원판 1: 2: 1: 3: 1: 2: 1: 4: 1: 2: 1: 3: 1: 2: 1: A A B A C C A A B B C B A A B => => => => B C C B A B B C C A A C B C C

점화식(recurrent formula) · 수열 an · 조합 int f(int n) { return (n <=

점화식(recurrent formula) · 수열 an · 조합 int f(int n) { return (n <= 1) ? 1 : f(n – 1) + (n – 1); } unsigned combination(unsigned n, unsigned r) { if (r == 0 || n == r) return 1; return combination(n – 1, r) + combination(n – 1, r – 1); } © 2012 생능출판사 All rights reserved