16
Computer Programming in C Computer Programming in C Chapter 1 Chapter 1 2004 년 년년년년 년년년년년 년년년년년년년년년년년년

Computer Programming in C Chapter 1

Embed Size (px)

DESCRIPTION

Computer Programming in C Chapter 1. 2004 년 가을학기 부산대학교 전자전기정보컴퓨터공학부. I 장 . 개요. 목차 컴퓨터 프로그래밍 언어와 C- 언어 프로그래밍 하는 절차 Sample 프로그래밍. 명령의 순서 : 프로그램. 1. 프로그래밍 언어와 C- 언어. 컴퓨터와 인간의 대화 : 프로그램 프로그램 : 컴퓨터에게 지시하는 명령의 순서 프로그래밍 언어 : 명령 또는 프로그램의 형식 , 문법 비교 : 일반 자연언어. 프로그래머. 컴퓨터. - PowerPoint PPT Presentation

Citation preview

Page 1: Computer Programming in C Chapter 1

Computer Programming in CComputer Programming in CChapter 1Chapter 1

2004 년 가을학기부산대학교

전자전기정보컴퓨터공학부

Page 2: Computer Programming in C Chapter 1

Computer Programming Chapter 1 2

II 장장 . . 개요개요

목차1. 컴퓨터 프로그래밍 언어와 C- 언어2. 프로그래밍 하는 절차3. Sample 프로그래밍

Page 3: Computer Programming in C Chapter 1

Computer Programming Chapter 1 3

1. 1. 프로그래밍 언어와 프로그래밍 언어와 C-C- 언어 언어

컴퓨터와 인간의 대화 : 프로그램

프로그램 : 컴퓨터에게 지시하는 명령의 순서 프로그래밍 언어 : 명령 또는 프로그램의 형식 , 문법

비교 : 일반 자연언어

컴퓨터프로그래머명령

명령의 순서 : 프로그램

Page 4: Computer Programming in C Chapter 1

Computer Programming Chapter 1 4

1. 1. 프로그래밍 언어와 프로그래밍 언어와 C-C- 언어 언어

C- 언어 1970 년대 초에 개발 초기에는 주로 Unix 를 위한 언어 컴퓨터 프로그래밍언어 중 가장 널리 사용 다른 언어에 비하여

프로그래머에 따라 효율성을 높이는 것이 가능 비교적 기계어에 가까운 언어

컴퓨터 엔지니어에게는 필수적 언어

Page 5: Computer Programming in C Chapter 1

Computer Programming Chapter 1 5

2. 2. 프로그래밍하는 절차프로그래밍하는 절차 단계 1 : 설계

프로그램의 구조 , 절차 등을 구상하고 정리 단계 2 : Editing 과 Coding

프로그램의 작성 주어진 환경의 편집 도구를 이용

예 . Unix: vi, emacs 예 . MS-Windows: Visual C/C++, Borland C

단계 3 : Compile 프로그램을 실행 가능한 기계어로 전환 Compiler 가 필요

예 . Visual C/C++, cc 단계 4 : 실행

실행 파일 (.exe) 을 실행

Page 6: Computer Programming in C Chapter 1

Computer Programming Chapter 1 6

2. 2. 프로그래밍하는 절차프로그래밍하는 절차

예 .

설계

Visual C/C++

Editing Compile

MS-Windows

Sample.c

실행

Sample.obj

Sample.exe 결과

Page 7: Computer Programming in C Chapter 1

Computer Programming Chapter 1 7

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Block

Page 8: Computer Programming in C Chapter 1

Computer Programming Chapter 1 8

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Variable

Value

Page 9: Computer Programming in C Chapter 1

Computer Programming Chapter 1 9

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Type Declaration

Page 10: Computer Programming in C Chapter 1

Computer Programming Chapter 1 10

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Statement

Page 11: Computer Programming in C Chapter 1

Computer Programming Chapter 1 11

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Control Flow

Page 12: Computer Programming in C Chapter 1

Computer Programming Chapter 1 12

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Condition

Page 13: Computer Programming in C Chapter 1

Computer Programming Chapter 1 13

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Assignment

Page 14: Computer Programming in C Chapter 1

Computer Programming Chapter 1 14

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Operator

Operator precedence : ( ) > *, / > …

Page 15: Computer Programming in C Chapter 1

Computer Programming Chapter 1 15

3. Sample Program3. Sample Program#include <stdio.h> #include <math.h>

main() { float a,b,c; /* ax2 + bx + c=0 */ float x1,x2; double D; /* input coefficients */ printf(“Input a b c :”); scanf(“%f %f %f”, &a, &b, &c); if(a==0) { if(b==0) { if(c==0) printf(“Any real number\n”); else printf(“No Solution\n”); }

else { /* b!=0 */ x1= -c/a; printf(“Solution x=%f\n”, x1); } else { /* a!=0 */ D = doubleb*b-4.0*a*c; if(D<0) printf(“No Solution\n”); else if(D==0) { x1=-b/(2.0*a); printf(“Solution x=%f\n”, x1); } else { x1=(-b+sqrt(D))/(2.0*a); x2=(-b-sqrt(D))/(2.0*a); printf(Solution x1=%f x2=%f\n”,x1,x2); } }}

Input/Output

Page 16: Computer Programming in C Chapter 1

Computer Programming Chapter 1 16

Basic Concepts of Sample Basic Concepts of Sample ProgramProgram Block : { … } Value, Variable Type Declaration : integer, float, double, etc. Statement and Semi-colon (“;”) Control Flow : if .. else if … else Condition Assignment (“=“) Operator, Operator Precedence Function Input and Output with printf and scanf