Function Argument Variable Data Type Statement Preprocessor Header
시작하기 전에 기본 예제 기본용어 함수(Function)와 인자(Argument) 변수(Variable)와 데이터형(Data Type) 문장(Statement) 선행처리기(Preprocessor)와 헤더 파일(Header File) #include <stdio. h> int main() { int year ; year = 2011; printf(“Welcome to CBNU, ”); printf(“Chung. Buk National University %d. n”, year); } return 0; 실행 결과 Welcome to CBNU, Chung. Buk National University 2010); slide-3
데이터 형 C의 기본 데이터 형 문자형, 정수형, 실수형 (부동 소수점형, 배 정밀도형) 문자형 기본 데이터형 정수형 실수형 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) slide-6
데이터 형 정수형(Integer Types) int 예) 2, -23, 2456, …. 일반적으로 사용중인 컴퓨터의 워드(Word) 크기이다. natural size of integers on the host machine[K&R] 예) IBM PC의 경우 2 bytes(16 bits) - 2의 16제곱 ~ +2의 16제곱 - 1 (-32768 ~ +32767) 변수의 선언과 초기화 예) int int sum; rows, colums; sum = 0; rows = 3, colums = 4; 비트(Bit), 바이트(Byte), 워드(Word) 비트(Bit) 바이트(Byte) : 일반적으로 1 byte = 8 bit 워드(Word) slide-7
데이터 형 정수형(Integer Types) 정수형 데이타의 출력 printf() 문 내에 형식 표시자(Format Specifier) 사용 %d %o %x 정수형 데이타를 10진수(Decimal)로 출력 정수형 데이타를 8 진수(Octal)로 출력 정수형 데이터를 16진수(Hexadecimal)로 출력 #include <stdio. h> int main() { int num = 23; printf(“Dec = %d, Oct = %o, Hex = %xn”, num, num); return 0; } [실행결과] Dec = 23, Oct = 27, Hex = 17 slide-8
데이터 형 정수형(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/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 slide-9
데이터 형 정수 한정자 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) slide-10
데이터 형 정수 한정자 저장 크기 알아내는 방법 : 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) ); } slide-11
데이터 형 기타 정수형의 출력 %u % ld % lo % lx % lu % hd %ho %hx unsigned int 형으로 출력 long int 형으로(10, 8, 16진수)출력 unsigned long int 형으로 출력 short int 형으로(10, 8, 16진수)출력 [예제 ] 기타 정수형의 출력(2 -byte 정수형의 경우) [실행결과 3. 3] #include <stdio. h> un = 40000 and not -25536 int main() { ln = 200000 and not -27648 unsigned un = 40000; uln = 400000 and not 10240 long ln = 200000 L; sn = 23 = 27(oct) = 17 (hex) unsigned long uln = 2 * 200000 L; short sn = 23; } printf(“un = %u and not %hdn”, un); printf(“ ln = %ld and not %hdn”, ln); printf(“ uln = %lu and not %hdn”, uln); printf(“ sn = %hd = %ho(oct) = %hx(hex)n”, sn, sn); return 0; slide-14
데이터 형 문자형(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. slide-17
데이터 형 문자형(Character type) [예제 ] 각 문자의 아스키 코드 값을 출력하는 프로그램 #include <stdio. h> int main() { int ch; for (ch=0; ch < 128; ch++) printf(“ASCII code for %c is %d. n”, ch); return 0; } [실행결과] slide-18
데이터 형 문자형(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. ” [예제 slide-19
데이터 형 부동소수점 형 데이타의 선언 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 slide-21
수치 결과 출력 프로그램 #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); } slide-26
수치 결과 출력 형식출력 : 필드 폭 변경자 프로그램 #include <stdio. h> void main(void) { printf(“n%d”, printf(“n---”); printf(“n%d”, } #include <stdio. h> void main(void) { printf(“n%3 d”, printf(“n---”); printf(“n%3 d”, } 6); 18); 124); 6+18+124); slide-27
수치 결과 출력 정형화된 부동소수점 수는 두 개의 필드 폭 변경자를 필요 Specifier Number Display Comments |%2 d| 3 | 3| Number fits in field |%2 d| 43 |43| Number fits in field |%2 d| 143 |143| Field width ignored |%2 d| 2. 3 Machine Floating point number dependent in an integer field | 2. 37| Field of 5 with 2 decimal digits |%5. 2 f| 2. 366 |%5. 2 f| 42. 3 |42. 30| Number fits in field |%5. 2 f| 142. 364 |142. 364| Field width ignored but fractional specifier used |%5. 2 f| 142 Machine dependent Integer in a floating point field slide-28
수치 결과 출력 프로그램 #include <stdio. h> void main(void) { printf(“The decimal (base 10) value of 15 is %d. ”, 15); printf(“n The octal (base 8) value of 15 is %o. ”, 15); printf(“n The hexadecimal (base 16) value of 15 is %x. ”, 15); } 프로그램 #include <stdio. h> void main(void) { printf(“The decimal value of 025 is %d. n”, 025); printf(“The decimal value of 0 x 37 is %d. n”, 0 x 37); } slide-30
수치 결과 출력 프로그램 #include <stdio. h> void main(void) { printf(“The decimal value of the letter %c is %d. ”, ‘a’); printf(“n. The octal value of the letter %c is %o. ”, ‘a’); printf(“n. The hex value of the letter %c is %x. ”, ‘a’); } slide-31
변수와 선언 프로그램 #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); } slide-34
변수와 선언 프로그램 #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); } slide-35
- Slides: 38