프로그래밍 기초와 실습 chapter 6 the fundamental data types

22
프프프프프 프프프 프프 Chapter 6 The Fundamental Data Types

Upload: neil-hunter

Post on 13-Dec-2015

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

프로그래밍 기초와 실습

Chapter 6

The FundamentalData Types

Page 2: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 2

Contents

Declarations and Expressions The Fundamental Data Types Characters and the Data Type char The Data Type int The Integral Types short, long, and unsigned The Floating Types The sizeof Operator Mathematical Functions Conversions and Casts

Page 3: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 3

Declarations and Expression

Declarations

– C 에서 모든 variable 은 사용 전에 declaration 되어야 한다 .

– 선언에 의해 compiler 가 variable 을 위한 memory 를 할당 .

– compiler 가 정확한 instruction 을 수행하도록 한다 .

[Ex] int month; int type 의 value 를 저장하기위한 memory(4B) 를 할당 => ‘month’ 에 의해 참조

[Ex] int a=1, b=2, c; float d=1.0, e=2.0, f;

c=a + b; d=e + f;

int + int 연산이므로결과가 int 가 됨

float + float 연산이므로결과가 float 가 됨

Page 4: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 4

Declarations and Expression

Expression– Expression 은 variables, constants, function call 로

이루어진다 .

– 모든 Expression 은 산출 결과인 value 와 그 value 의 type 을 갖는다 .

– Expression 의 type 은 구성 요소인 constants, variable, function return type 에 의해 결정된다 .[Ex] int a=1, b=2; float d=1.0, e=2.0;

a + b; /* type : int, value : 3 */ e + f; /* type : float, value : 3.0 */ 3.0 +2.0; /* type : double, value : 5.0 실수형의 constant 는 double */

Page 5: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 5

The Fundamental Data Types

C 에서 제공하는 Data Types

– 모든 type 은 keyword 이다 . (identifier 로 사용할 수 없다 .)

– ‘signed’ keyword 는 생략 가능 .

– short int, long int, unsigned int 에서 int 의 생략 가능 : short, long, unsigned 로 사용

Fundamental data types

char signed char unsigned char

signed short int signed int signed long int

unsigned short int unsigned int unsigned long int

float double long double

[Ex] ‘int’ 와 ‘ signed int’ 는 같은 의미

Page 6: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 6

The Fundamental Data Types

기능별로 분류

Fundamental types grouped by functionality

Integral types char signed char unsigned char

short int long

unsigned short unsigned unsigned long

Floating types float double long double

Arithmetic types Integral types + Floating types

Page 7: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 7

Characters and the Data Type char Characters

– 모든 integral type 의 변수는 character 의 저장 가능

– 한 character 를 저장하기 위해 int 또는 char 로 선언 .

– Character Constant 는 ASCII code 값으로 전환되므로 int 와 호환 가능하다 .

[Ex1] int c;c= ‘A’+5; /* ‘A’ 의 ASCII code : 65 */printf(“%c %d\n”, c, c);

[Ex2] c= ‘A’;printf(“%c %d %c %d\n”, c, c, ++c, c);

[Ex3] for(c= ‘A’; c<= ‘Z’; c++) printf(“%c ”,c);

F 70

A 65 B 66

A B C D…X Y Z

Page 8: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 8

Characters and the Data Type char

The Data Type char– C 에서 char type 은 1byte(8bit) 의 메모리를 할당 .

– 8bit 는 28 = 256 개의 character 를 표현할 수 있다 .

– char type 의 값의 범위

• char(signed char) : -128(-27) ~ 127(27-1)

• unsigned char : 0 ~ 255

[Ex] char c = ‘a’ ; /* ‘a’ 97 => 01100001(2) */

실제 메모리에 저장되는 형태

0 1 1 0 0 0 0 1

7 6 5 4 3 2 1 0

Page 9: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 9

The Data Type int

The Data Type int – machine 의 word size 에 따라 할당되는 메모리 size 결정

• 2bytes machine : -32768(-215) ~ 32767(215-1)

• 4bytes machine : -2147483648(-231) ~ 2147483647(231-1)[Ex]

#include <stdio.h> #define BIG 2000000000

int main(void) {

int a, b=BIG, c=BIG;

a = b + c; printf("a=%d\n", a); return 0;

}

Overflow 발생 : int type 이 표현할 수 있는 최대값보다 큰 값이므로정확한 값이 저장될 수 없다 .

a=-294967296

Page 10: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 10

The Integral Types – short, long, unsigned

short

– 4bytes machine 일 경우 int 보다 적은 메모리 사용 (2bytes)

– range : -32768(-215) ~ 32767(215-1)

– 메모리 size 가 문제되는 경우 사용

long– 2bytes machine 일 경우 int 보다 더 큰 수를 표현 할 수 있

다 .

(4bytes 메모리 사용 )

– 4bytes machine : -2147483648(-231) ~ 2147483647(231-1)

Page 11: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 11

The Integral Types

unsigned– 양의 정수 만을 저장

– unsigned 의 값의 범위 (0 ~ 2wordsize-1)

• 2bytes : 0 ~ 65535(216-1)

• 4bytes : 0~ 42949647295(232-1)

suffixes– 특별한 type 의 integral constant 만들기 위하여 사용

combining long and unsigned

Suffix Type Example

u or U unsigned 37U

l or L long 37L

ul or UL unsigned long 37UL

Page 12: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 12

The Floating Types

float, double, long double– 실수 형의 data 저장

– integral type 과 달리 정확한 값이 아닌 근사값의 표현

– suffix 가 없는 floating constant type 은 double 이다 .

– Exponential Notation

combining long and unsigned

Suffix Type Example

f or F float 3.7F

l or L long double 3.7L

[Ex] 1.234567e5 = 1.234567 x 105integer : 1fraction : 234567exponent : 5

Page 13: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 13

The Floating Types

float– 4bytes 의 메모리 할당

– 유효숫자 6 자리의 정확도

– 값의 범위 : 10-38 ~ 1038

double– 8bytes 의 메모리 할당

– 유효숫자 15 자리의 정확도

– 값의 범위 : 10-308 ~ 10308

[Ex] double a = 123.45123451234512345; /* 20 digits */

실제 메모리에 저장되는 데이터a = 0.123451234512345e3 /* 15digts */

Page 14: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 14

The sizeof Operator

The sizeof Operator– variable 에 할당된 메모리 size, type 에 따른 메모리 size

를 byte 단위로 표현

– unary operator 의 precedence, associativity 를 갖는다 .

– value type 은 unsigned int 이다 .[Ex]

int i,j;

sizeof(char); /* 1 */sizeof(int); /* 4 */

sizeof(45); /* 4 */ sizeof(4.5); /* 8 */ sizeof(i); /* 4 */ sizeof(int j); /* 4 */

integer Constant

double Constant

type : unsigned

Page 15: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 15

Mathematical Functions

Mathematical Functions

– sqrt(), pow(), exp(), log(), sin(), cos(), tan() 등… .

– Header file “math.h” 의 삽입 필요 : Function Prototypes 존재

– 모든 math function 은 double type 의 argument 와 double type 의 return value 를 갖는다 .

– Compile 시 math library 를 link 하기 위해 “-lm” 을 옵션으로 주어야 한다 .[Ex] cc filename.c -lm

[Ex] double sin(double); /* 모든 angle 은 radian 으로 표시 */

[Ex] double pow(double, double);

Page 16: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 16

Mathematical Functions

[Ex] #include <stdio.h>#include <math.h>

#define PI 3.1415926

int main() { double r = PI / (2*90); /* 1 도에 해당하는 radian 값 */ int i;

for(i=0; i<=90; i+=5) { printf("cos(%d) = %f\t", i, cos(r*i)); printf("sin(%d) = %f\t", i, sin(r*i)); printf("tan(%d) = %f\n", i, tan(r*i)); }

}cos(0) = 1.000000 sin(0) = 0.000000 tan(0) = 0.000000…cos(45) = 0.707107 sin(45) = 0.707107 tan(45) = 1.000000…cos(90) = 0.000000 sin(90) = 1.000000 tan(90) = 37320539.634355

Page 17: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 17

Conversions and Casts

The Integral Promotion

– 모든 integral type 은 expression 에서 자동 int 로 convert 된다 .

[Ex] short + short → intchar + char → intunsigned short + unsigned short → unsigned

[Ex] char c = ‘a’;printf(“%d, %c\n”, c, c);

Char 를 %d format 으로 출력 시 자동 int 로 convert

97, a

Page 18: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 18

Type Promotion in Expression

– Expression 에서 다른 type 의 operands 사용 시 자동 largest operand type 으로 convert

Conversions and Casts

float double long double

int unsigned int long int unsigned long int

int 와 double 의 혼용 전부 double 로 convert

[Ex] int a=1, b=2; double x=3.0, z; float y=4.0;

z = a + b + x + y; /* 모든 operands 의 type 이 double 로 convert 되어 계산됨 */

Page 19: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 19

Conversions and Casts

Conversion during Assignment

– Assignment statement 에서 right side 는 left side 의 type 으로 자동 convert

[Ex] int i;double d=3.7

i = d; /* i = 3 */d = i; /* d = 3.0 */

left side 가 int type 이므로 3.7 이 int type 으로 convert=> 소수점 이하 자리는 잘림 .

left side 가 double type 이므로 ,double type 인 3.0 으로 convert

int = char; /* int 로 변환 */ double = int; /* double 변환 */ int = float; /* int 로 변환 */ ...

Page 20: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 20

Conversions and Casts

Casts– expression 에서 operand 의 type 을 convert

– (type 명 )expression

[Ex1] int a=3, b=2;double c = a / b;

printf(“c=%f\n”, c);

[Ex2] int a=3, b=2;double c = (double) a / b;

printf(“c=%f\n”, c);

c=1.000000

c=1.500000

a,b 모두 int 이므로 (int/int) 의 결과는 int 이며 left side type 인 double type 으로 convert. 3/2 = 1 ==> 1.0 이 c 에 assign 됨

a 는 double 로 cast 되어 (dobule/int) expression 이 되고int 가 double 로 promotion 된다 .(double/double) 의 결과는 double 3.0 / 2.0 = 1.5

Page 21: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

Chapter 6 The Fundamental Data Types 21

Conversions and Casts

[Ex3] int a=3, b=2;

double c = (double) (a / b);

printf(“c=%f\n”, c);

c=1.000000

a, b 모두 int 이므로 (int/int) 의 결과는 int. 3/2 = 1그리고 그 결과를 double 로 cast 하므로 1.0 이 c 에 assign 됨

Page 22: 프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types

수고하셨습니다

The FundamentalData Types