include stdio h int main int year Function

  • Slides: 33
Download presentation

시작하기 전에 #include <stdio. h> int main() { int year ; 기본용어 함수(Function)와 인자(Argument)

시작하기 전에 #include <stdio. h> int main() { int year ; 기본용어 함수(Function)와 인자(Argument) 변수(Variable)와 데이터형(Data Type) 문장(Statement) 선행처리기(Preprocessor)와 헤더 파일(Header File) year = 2001; printf(“Welcome to CNU, ”); printf(“Chungbuk National University %d. n”, year); } return 0; 실행 결과 ? Ch 02. 데이터형 선언 및 출력 3

데이터 형 C의 기본 데이터 형 문자형, 정수형, 실수형 (부동 소수점형, 배 정밀도형) 문자형

데이터 형 C의 기본 데이터 형 문자형, 정수형, 실수형 (부동 소수점형, 배 정밀도형) 문자형 기본 데이터형 정수형 실수형 Ch 02. 데이터형 선언 및 출력 char(1 byte) unsigned char(1 byte) short int(2 byte) unsigned char(2 byte) int(4 byte) unsigned int(4 byte) long int(4 byte) unsigned long int(4 byte) float(4 byte) double(8 byte) 6

데이터 형 정수형(Integer Types) 그 외의 정수형 short, long, unsigned와 같은 키워드를 int 앞에

데이터 형 정수형(Integer Types) 그 외의 정수형 short, long, unsigned와 같은 키워드를 int 앞에 붙임 short int 또는 short long int 또는 long unsigned int 또는 unsigned 부호를 무시함으로써 양수의 범위로 2배로 사용 예) 2 -byte 정수형에서 unsigned int : 0 ~ 65535 short (2 bytes) <= int <= (2 or 4 bytes) long (4 bytes) (단위 : 바이트) 여러 가지 기종에서의 정수형 데이터의 크기 Macintosh IBM PC ANSI C (Think C) (MSC, BC, VC) Minimum 데이터형 DEC VAX short 2 2 int 4 2 2(4) 2 long 4 4 Ch 02. 데이터형 선언 및 출력 9

데이터 형 정수 한정자 long, short, unsigned : int 선언문 앞에 붙여 표현할 수

데이터 형 정수 한정자 long, short, unsigned : int 선언문 앞에 붙여 표현할 수 있는 정수 숫자 의 크기를 정하는 것 Data Type Storage Number Range 2 Bytes Integer Unsigned Integer -32768 to 32767 (a total of 65, 536 numbers) 0 to 65, 535 (a total of 65, 536 numbers) Ch 02. 데이터형 선언 및 출력 10

데이터 형 정수 한정자 저장 크기 알아내는 방법 : sizeof() 연산자 예제 #include <stdio.

데이터 형 정수 한정자 저장 크기 알아내는 방법 : sizeof() 연산자 예제 #include <stdio. h> void main(void) { long num 0; int num 1; printf(“Bytes of storage used by a long integer : %d”, sizeof(num 0) ); printf(“n. Bytes of storage used by an integer : %d”, sizeof(num 1) ); } Ch 02. 데이터형 선언 및 출력 11

데이터 형 오버플로우 에러 (예) 32767: 0 1 1 1 1 -32768: 1 0

데이터 형 오버플로우 에러 (예) 32767: 0 1 1 1 1 -32768: 1 0 0 0 0 -32767: 1 0 0 0 0 1 각 비트별 가중치 : 예) -215 214 213 212 211 210 29 28 27 26 25 24 23 22 21 20 7: 0 1 1 1 = -23*0 + 22*1 + 21*1 + 20*1 = 4+2+1= 7 -8: 1 0 0 0 = -23*1 + 22*0 + 21*0 + 20*0 = -8 -7: 1 0 0 1 = -23*1 + 22*0 + 21*0 + 20*1 = -8+1= -7 -23 22 21 20 Ch 02. 데이터형 선언 및 출력 13

데이터 형 문자형(Character type) 문자형의 출력 %c 문자로 출력 (%d 를 사용하면 ASCII 값을

데이터 형 문자형(Character type) 문자형의 출력 %c 문자로 출력 (%d 를 사용하면 ASCII 값을 출력) [예제] #include <stdio. h> int main() { char ch; } [실행결과 ] printf(“Please enter a character. n”); scanf(“%c”, &ch); printf(“ASCII code for %c is %d. n”, ch); return 0; Please enter a character. A ASCII code for A is 65. Ch 02. 데이터형 선언 및 출력 16

데이터 형 문자형(Character type) 예외 문자 (Escape Sequence) 역슬래쉬, ’’와 몇몇 문자들이 조합되어 특별한

데이터 형 문자형(Character type) 예외 문자 (Escape Sequence) 역슬래쉬, ’’와 몇몇 문자들이 조합되어 특별한 의미를 가지는 문자 를 표현 Sequence n t b r f \ ’ ” ooo xhh Meaning newline or line feed tab backspace carriage return form feed backspace() itself single quote(‘) double quote(“) Octal value(o is an octal digit) Hexadec. Value(h is a hexadec. digit) [예제 ] 부분 프로그램 printf(“He said, ” a \ is a backslach. ”n”); [실행결과] He said, ” a is a backslash. ” Ch 02. 데이터형 선언 및 출력 18

데이터 형 부동소수점 형 데이타의 선언 float average = 0. 0; double sdev =

데이터 형 부동소수점 형 데이타의 선언 float average = 0. 0; double sdev = 0. 0; 부동소수점 형 데이타의 출력 %f %e decimal notation 형태로 출력 exponential notation 형태로 출력 [예제] #include <stdio. h> main() { float average = 32000. 0; printf(“%f can be written %en”, average); return 0; } [실행결과] 32000. 00000 can be written 3. 200000 e+004 Ch 02. 데이터형 선언 및 출력 20

수치 결과 출력 프로그램 #include <stdio. h> void main(void) { printf(“%f plus %f equals

수치 결과 출력 프로그램 #include <stdio. h> void main(void) { printf(“%f plus %f equals %fn”, 15. 0, 2. 0, 15. 0 + 2. 0); printf(“%f minus %f equals %fn”, 15. 0, 2. 0, 15. 0 - 2. 0); printf(“%f times %f equals %fn”, 15. 0, 2. 0, 15. 0 * 2. 0); printf(“%f divided by %f equals %fn”, 15. 0, 2. 0, 15. 0 / 2. 0); } Ch 02. 데이터형 선언 및 출력 25

수치 결과 출력 형식출력 : 필드 폭 변경자 프로그램 1 프로그램 2 #include <stdio.

수치 결과 출력 형식출력 : 필드 폭 변경자 프로그램 1 프로그램 2 #include <stdio. h> void main(void) { printf(“n%d”, 6); printf(“n%d”, 18); printf(“n%d”, 124); printf(“n-----”); printf(“n%d”, 6+18+124); } 6 [결과] 18 124 ----148 #include <stdio. h> void main(void) { printf(“n%3 d”, 6); printf(“n%3 d”, 18); printf(“n%3 d”, 124); printf(“n-----”); printf(“n%3 d”, 6+18+124); } 6 [결과] 18 124 ----148 Ch 02. 데이터형 선언 및 출력 26

변수와 선언 컴퓨터에서 데이터를 사용하는 방법 Storage for one integer Address : 1321 2649

변수와 선언 컴퓨터에서 데이터를 사용하는 방법 Storage for one integer Address : 1321 2649 1322 2650 변수의 정의 단순히 프로그래머가 컴퓨터 저장 공간에 지어준 이름 메모리 주소 대신 기호 이름이 사용되며, 이 기호 이름을 변수라고 함 Storage for one integer num 1 result Address : Storage for one integer 45 46 Storage for one integer num 2 1321 1322 Ch 02. 데이터형 선언 및 출력 2649 2650 27

변수와 선언 프로그램 #include <stdio. h> void main(void) { float grage 1; float grade

변수와 선언 프로그램 #include <stdio. h> void main(void) { float grage 1; float grade 2; float total; float average; grade 1 = 85. 5; grade 2 = 97. 0; total = grade 1 + grade 2; average = total/2. 0; printf(“The average grade is %fn”, average); } Ch 02. 데이터형 선언 및 출력 30

변수와 선언 프로그램 #include <stdio. h> void main(void) { char ch; ch = ‘a’;

변수와 선언 프로그램 #include <stdio. h> void main(void) { char ch; ch = ‘a’; printf(“n. The character stored in ch is %c. ”, ch); ch = ‘m’; printf(“n. The character now stored in ch is %c. ”, ch); } Ch 02. 데이터형 선언 및 출력 31