basis and practice in programming lecture notes (2/18)

34
1 C 프로그램의 기본 구성 Prof. Seungjoo Kim Security Analysis aNd Evaluation Lab. Department of Cyber Defense Korea University http://www.KimLab.net

Upload: seungjoo-kim

Post on 09-Feb-2017

98 views

Category:

Engineering


5 download

TRANSCRIPT

Page 1: Basis and Practice in Programming Lecture Notes (2/18)

1

C 프로그램의 기본 구성

Prof. Seungjoo Kim Security Analysis aNd Evaluation Lab.

Department of Cyber Defense Korea University

http://www.KimLab.net

Page 2: Basis and Practice in Programming Lecture Notes (2/18)

2

이것이 C 언어다.

Page 3: Basis and Practice in Programming Lecture Notes (2/18)

3

1972년 Bell lab에서 개발 System level의 programming Unix OS를 다양한 하드웨어에서 사용할 수 있도록 하기 위해서 개발

원류는 BCPL (Basic Combined Programming Language) (Martin Richards) B language (Ken Thompson) C language (Dennis Ritchie) C++, Visual C++ …

C언어의 역사

켄 톰프슨과 데니스 리치

Page 4: Basis and Practice in Programming Lecture Notes (2/18)

4

High Level Language이면서 Low Level의 특성 소유. bit, byte, word 단위의 조작 가능

Simple and small language, translatable with simple and small compilers

다양한 응용 프로그램을 구현하는 언어

C언어의 특성

Page 5: Basis and Practice in Programming Lecture Notes (2/18)

5

Efficient Machine level 의 개념으로 개발된 프로그래밍 언어로 신속한 수행가능

Portability

PC에서 Supercomputer에 이르기까지 적용 가능

Powerful 다양한 데이터 타입과 연산자를 제공

C언어의 장점

Page 6: Basis and Practice in Programming Lecture Notes (2/18)

6

Flexibility 시스템 프로그래밍으로부터 모든 응용 프로그래밍까지 적용가능

다양한 Standard Library제공

Input/Output, String handling, Storage allocation등을 위한 수 많은 library functions을 제공

C언어의 장점

Page 7: Basis and Practice in Programming Lecture Notes (2/18)

7

Error Prone C언어의 flexibility에 따른 Compiler에 의한 error감지 어려움

Difficulty 다양한 기능의 포함으로 이해 또는 수정하기가 어려움

C언어의 단점

Page 8: Basis and Practice in Programming Lecture Notes (2/18)

8

프로그램 작성 및 실행 순서

1. Goal 설정 2. Algorithm 작성 3. 프로그램 작성 4. 컴파일 및 링크 5. Testing 및 Debugging 6. 실행파일 생성

프로그램의 완성 과정

Page 9: Basis and Practice in Programming Lecture Notes (2/18)

9

프로그램의 완성 과정

Source

File

Preprocessed (확장된) Source

File

Object File 그리고 필요하다면

Library File

Executable File

Preprocessing Compile Link

Preprocessor Compiler Linker

Header File Library

Page 10: Basis and Practice in Programming Lecture Notes (2/18)

10

Visual C++ 컴파일러 초기 화면 및 메뉴

프로그램의 완성 과정

Page 11: Basis and Practice in Programming Lecture Notes (2/18)

11

컴파일러 (compiler)

번역방식 : 프로그램 전체 번역

장점 : 한번 번역하면 빠른 시간내에 전체를 실행가능

단점 : 프로그램의 일부를 수정하는 경우에는 전체를 컴파일해야 함

결과물 : 목적언어로 된 프로그램

적용언어 : FORTRAN, PASCAL, COBOL, ADA, C

컴파일러와 인터프리터

Page 12: Basis and Practice in Programming Lecture Notes (2/18)

12

인터프리터 (interpreter)

번역방식 : 실행되는 행 단위로 번역

장점 : 큰 기억장치가 필요하지 않으며 번역과정이 비교적 간단함.

단점 : 반복문이 많은 경우 매 반복때마다 번역해야 함

결과물 : 실행의 결과

적용언어 : Prolog, Snobol4, Lisp, Basic

컴파일러와 인터프리터

Page 13: Basis and Practice in Programming Lecture Notes (2/18)

13

개발의 편의성

기타 라이브러리 등의 사용이 편해짐

보안 유지

컴파일과 링크의 분리 이유

Page 14: Basis and Practice in Programming Lecture Notes (2/18)

14

라이브러리 (Library)

특정한 코드(함수 혹은 클래스)를 포함하고 있는 컴파일된 파일.

자주 사용되는 특정한 기능을 main 함수에서 분리시켜 놓음으로써, 프로그램의 유지 및 디버깅을 쉽게 하고 컴파일 시간을 좀더 빠르게 할 수 있음.

라이브러리의 종류

정적 라이브러리

동적 라이브러리 (DLL)

라이브러리란? (23쪽 참조)

Page 15: Basis and Practice in Programming Lecture Notes (2/18)

15

동적 라이브러리 (DLL, Dynamic Link Library)

DLL 파일들의 장점은 그들이 주 프로그램과 함께 램(RAM)에 적재되지 않기 때문에 램 공간을 절약한다는데 있음. DLL 파일은 필요한 경우에만 적재되어 실행됨.

예를 들어 MS 워드 사용자가 문서를 편집하고 있는 동안에, 프린터의 DLL 파일은 램에 적재되어야 할 필요가 없음. 만약 사용자가 문서를 출력하려고 결심하면, 워드 프로그램은 그제야 프린터 DLL 파일을 적재, 실행시킴.

DLL 파일은 거의 ".dll"이라는 파일 확장자를 가짐.

라이브러리란?

Page 16: Basis and Practice in Programming Lecture Notes (2/18)

16

프로그램의 기본 구성

Page 17: Basis and Practice in Programming Lecture Notes (2/18)

17

"Hello, World!" 들여다 보기

/* Hello.c */ #include <stdio.h> int main(void) { printf("Hello, World! \ n"); return 0; }

Page 18: Basis and Practice in Programming Lecture Notes (2/18)

18

함수에 대한 이해

적절한 입력과 그에 따른 출력이 존재 하는 것을 가리켜 함수라 한다.

C 언어의 기본 단위는 함수이다.

"Hello, World!" 들여다 보기

그림 2-1

Page 19: Basis and Practice in Programming Lecture Notes (2/18)

19

함수 호출과 인자 전달 인자 전달 : 입력 x를 전달하는 행위 함수 호출 : 인자를 전달하면서 함수의 실행을 요구하는 행위

C 언어의 함수 특성 입력과 출력 존재 순차적으로 실행 함수의 기능을 정의하는 몸체 부분 존재

"Hello, World!" 들여다 보기

Page 20: Basis and Practice in Programming Lecture Notes (2/18)

20

예제 Hello.c에서의 함수

"Hello, World!" 들여다 보기

그림 2-3

Page 21: Basis and Practice in Programming Lecture Notes (2/18)

21

세미콜론이 필요한 문장 연산을 수행하는 문장 : 시간의 흐름에 따라서 컴퓨터에게 "이러 이러한 일을 해라"라고 명령을 하는 문장

표준 라이브러리에 대한 이해 이미 표준화 해서 만들어 놓은 함수들의 집합을 가리켜 표준 라이브러리라 한다.

헤더 파일을 포함해야 사용이 가능하다.

"Hello, World!" 들여다 보기

Page 22: Basis and Practice in Programming Lecture Notes (2/18)

22

헤더 파일의 이해 stdio.h 라는 이름의 헤더 파일 헤더 파일의 포함을 알리는 선언은 제일 먼저 등장해야 한다.

"Hello, World!" 들여다 보기

그림 2-4

Page 23: Basis and Practice in Programming Lecture Notes (2/18)

23

return의 의미 함수를 종료(빠져 나온다). 함수를 호출한 영역으로 값을 반환

return의 특징

return은 함수 내에서 존재 하지 않을 수도 있다. 둘 이상의 return문이 존재하는 것도 가능

"Hello, World!" 들여다 보기

Page 24: Basis and Practice in Programming Lecture Notes (2/18)

24

주석이란?

프로그래머에게 메모(memo)의 기능을 부여 컴파일러는 주석을 없는 것으로 간주 주석을 삽입 함으로 인해 프로그램의 가독성 증가

선택이 아닌 필수!

주석에 대한 이해

Page 25: Basis and Practice in Programming Lecture Notes (2/18)

25

주석의 두 가지 형태 여러 줄에 걸친 주석 처리

단일 행 주석 처리

주석에 대한 이해

/* 한 줄 짜리 주석 */ /* 여러 줄에 걸친 주석 */

// 주석 하나. // 주석 둘. // 주석 셋.

Page 26: Basis and Practice in Programming Lecture Notes (2/18)

26

주석의 예

주석에 대한 이해

#include <stdio.h> // stdio.h 헤더 파일 포함 int main(void) // main 함수의 시작 { /* printf 함수는 모니터로 출력을 하는 경우에 쓴다. 인자로 문자열을 전달하면 문자열을 출력한다. */ printf("Hello World! \ n"); //모니터로 문자열 출력 return 0; // 0을 반환한다. } // main 함수의 끝

Page 27: Basis and Practice in Programming Lecture Notes (2/18)

27

주석 처리에 있어서의 주의점

주석을 나타내는 기호는 중복될 수 없다.

단, 단일 행 주석은 중복 가능하다.

주석에 대한 이해

/* 주석의 시작, 여러 행에 걸쳐서 /* 단일 행 주석 처리 */ */

/* 주석의 시작, 여러 행에 걸쳐서 // 단일 행 주석 처리 */

Page 28: Basis and Practice in Programming Lecture Notes (2/18)

28

printf 함수 사용의 예 1

printf 함수의 기본적 이해

/* printf1.c */ #include <stdio.h> int main(void) { printf("Hello Everybody \ n"); printf("%d \ n", 1234); printf("%d %d \ n", 10, 20); return 0; }

Page 29: Basis and Practice in Programming Lecture Notes (2/18)

29

printf 함수 호출의 이해 1

printf 함수의 기본적 이해

printf("Hello Everybody \ n");

그림 2-5

Page 30: Basis and Practice in Programming Lecture Notes (2/18)

30

printf 함수 호출의 이해 2 : 서식 문자

서식 문자(Conversion specifier)란 출력 대상의 출력 형태를 지정하기 위한 문자

printf 함수의 기본적 이해

printf("%d \ n", 1234);

그림 2-6

Page 31: Basis and Practice in Programming Lecture Notes (2/18)

31

printf 함수 호출의 이해 3

printf 함수의 기본적 이해

printf("%d %d \ n", 10, 20);

그림 2-7

Page 32: Basis and Practice in Programming Lecture Notes (2/18)

32

printf 함수 사용의 예 2

printf 함수의 기본적 이해

/* printf2.c */ #include <stdio.h> int main(void) { printf("My age : %d \ n", 20); printf("%d is my point \ n", 100); printf("Good \ nmorning \ neverybody\ n"); return 0; }

Page 33: Basis and Practice in Programming Lecture Notes (2/18)

33

printf 함수 호출의 이해 4

printf 함수의 기본적 이해

그림 2-8

Page 34: Basis and Practice in Programming Lecture Notes (2/18)

34

Q & A