C char1 byte unsigned char1 byte short int2
데이터 형 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-4
데이터 형 정수형(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, Dct = 27, Hex = 17 slide-5
데이터 형 기타 정수형의 출력 %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-6
데이터 형 문자형(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-8
데이터 형 부동소수점 형 데이타의 선언 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-10
변수와 선언 프로그램 #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-12
- Slides: 12