27
Variables & Fundamental Data Types

2 1. variables & data types

  • Upload
    -

  • View
    232

  • Download
    6

Embed Size (px)

Citation preview

Page 1: 2 1. variables & data types

Variables & Fundamental Data Types

Page 2: 2 1. variables & data types

Structure of C program

Start with #include <..>

All statements locate between “int main(void) {“ and “}”

All statements end with “;”

Case sensitive – “Printf” and “printf” are not the

same

2

#include <stdio.h> int main(void) { int x ; scanf( “%d”, &x ) ; printf( “%d\n”, x*x ) ; return 0; }

Page 3: 2 1. variables & data types

Structure of C program

3

#include <stdio.h> int main(void) { int x ; scanf( “%d”, &x ) ; printf( “%d\n”, x*x ) ; return 0; }

Variable declaration

Input

Output

Page 4: 2 1. variables & data types

Variables

Variables (변수)

– 프로그램 실행 중에 값을 저장하기 위한 기억장소.

– 변수는 사용되기 전에 선언되어야 함.

– 프로그램 실행 중 변수는 메모리에 생성됨.

4

#include <stdio.h>

int main(void)

{

int inches, feet, fathoms;

}

inches

feet

fathoms

Page 5: 2 1. variables & data types

Variables

Variables Naming Rule

– 영문자 또는 _로 시작.

– 영문자, 숫자, _(underbar) 로 구성.

– maximum 255자 까지 가능.

– 예약어(Reserved workds)는 변수이름으로 사용할 수 없음.

5

[Ex]

사용 가능한 변수명 : times10, get_next_char, _done

사용 불가능한 변수명 : 10times, get-next-char, int

Page 6: 2 1. variables & data types

Variables

Reserved Words

– C언어의 일부로 의미와 사용처가 이미 결정된 단어들.

6

Keywords

auto do goto signed unsigned

break double if sizeof void

case else int static volatile

char enum long struct while

const extern register switch

continue float return typedef

default for short union

Page 7: 2 1. variables & data types

Variables

그런데, 변수 앞에 있는 int는 무엇인가??

7

#include <stdio.h>

int main(void)

{

int inches, feet, fathoms;

}

변수가 저장할 수 있는 값을 종류를 의미함

Page 8: 2 1. variables & data types

8

The Fundamental Data Types

C에서 제공하는 Data Types

– ‘signed’ keyword는 생략 가능

• int 와 signed int, long과 signed long 등은 각각 같은 의미

– 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

Page 9: 2 1. variables & data types

9

The Data Type int

int : – 2 byte machine : -32768(-215) ~ 32767(215-1) – 4 byte machine : -2147483648(-231) ~ 2147483647(231-1) – 8 byte machine : -2147483648(-231) ~ 2147483647(231-1)

short

– 2 byte machine : -32768(-215) ~ 32767(215-1) – 4 byte machine : -32768(-215) ~ 32767(215-1) – 8 byte machine : -32768(-215) ~ 32767(215-1)

long – 2 byte machine : -2147483648(-231) ~ 2147483647(231-1) – 4 byte machine : -2147483648(-231) ~ 2147483647(231-1) – 8 byte machine : -263 ~ (263-1)

Page 10: 2 1. variables & data types

10

The Integral Types

unsigned: 양의 정수 만을 저장

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

• 2 byte machine: 0 ~ 65535(216-1)

• 4 byte machine: 0~ 42949647295(232-1)

• 8 byte machine: 0~ 42949647295(232-1)

– unsigned long의 값의 범위

• 2 byte machine: 0~ 42949647295(232-1)

• 4 byte machine: 0~ 42949647295(232-1)

• 8 byte machine: 0 ~ (264-1)

Page 11: 2 1. variables & data types

11

The Integral Types

양수와 음수의 비트 표현 : 2 bytes int의 경우

0000 0000 0000 0000 -> 0

0000 0000 0000 0001 -> 1

0000 0000 0000 0010 -> 2

0000 0000 0000 0011 -> 3

0111 1111 1111 1110 -> 215-2

0111 1111 1111 1111 -> 215-1

1000 0000 0000 0000 -> -215

1000 0000 0000 0001 -> -215+1

1000 0000 0000 0010 -> -215+2

1111 1111 1111 1101 -> -3

1111 1111 1111 1110 -> -2

1111 1111 1111 1111 -> -1

0000 0000 0000 0000 -> 0

0000 0000 0000 0001 -> 1

0000 0000 0000 0010 -> 2

0000 0000 0000 0011 -> 3

0111 1111 1111 1110 -> 215-2

0111 1111 1111 1111 -> 215-1

1000 0000 0000 0000 -> 215

1000 0000 0000 0001 -> 215+1

1000 0000 0000 0010 -> 215+2

1111 1111 1111 1101 -> 216-3

1111 1111 1111 1110 -> 216-2

1111 1111 1111 1111 -> 216-1

int의 경우 unsinged int의 경우

Page 12: 2 1. variables & data types

12

The Integral Types

예제 : 4 byte machine

int i = 2147483645, j ; for( j = 0 ; j < 5 ; j++ ) printf( “%d\n”, i + j ) ;

unsigned int i = 2147483645, j ; for( j = 0 ; j < 5 ; j++ ) printf( “%u\n”, i + j ) ;

2147483645 2147483646 2147483647 -2147483648 -2147483647

2147483645 2147483646 2147483647 2147483648 2147483649

Page 13: 2 1. variables & data types

13

The Integral Types

예제 : 4 byte machine

int i = -1 ; unsigned u = -1 ; printf( “%d %u\n”, i, u ) ; printf( “%d %d\n”, i, u ) ; printf( “%u %u\n”, i, u ) ;

-1 4294967295 -1 -1 4294967295 4294967295

int 변수에 %u를 사용하면, int를 unsigned int 취급한다.

unsigned int 변수에 %d를 사용하면, unsigned int를 int 취급한다.

Page 14: 2 1. variables & data types

14

Integer Constants

Integer Constants :

– C에서 정수형은 Decimal, Octal, Hexadecimal로 표현된다.

[Ex] 17 /* decimal integer constant */ 017 /* octal integer constant : 17(8) = 15 */ 0x17 /* hexadecimal integer constant 17(16)= 23 */ -17 /* negative decimal integer constant */

Page 15: 2 1. variables & data types

15

Integer Constants

Example

#include <stdio.h> int main(void) { int i = 17, j = 017, k =0x17; printf( “%d %d %d\n”, i, j, k ); return 0; }

17 15 23

#include <stdio.h> int main(void) { int i = 15; printf( “%d %o %x %X\n”, i, i, i, i ); return 0; }

15 17 f F

Page 16: 2 1. variables & data types

16

The Data Type char

Char type

– 8bit의 ASCII code로 표현

– 총 256 개의 char 표현 가능

– 문자 또는 작은 수의 int로 표현

[Ex]

printf(“%c”, ‘a’ ); /* a is printed */

printf(“%c%c%c”, ‘A’, ‘ B’, ‘C’ ); /* ABC is printed */

printf(“%c”, 97 ); /* a is printed */

printf(“%c”, ‘a’+1 ); /* b is printed */

printf(“%d”, ‘a’ ); /* 97 is printed */

Page 17: 2 1. variables & data types

17

The Data Type char

모든 정수형 수식은 문자형태나 정수형태로 나타낼 수 있다.

[Ex]

char c; int i;

for ( i = ‘a’ ; i <= ‘z’; ++i )

printf(“%c”, i); /* abc … z is printed */

for ( c = 65; c <= 90 ; ++c )

printf(“%c”, c); /*ABC … Z is printed */

for ( c = ‘0’; c <= ‘9’ ; ++c )

printf(“%d ”, c); /* 48 49 50… 57 is printed */

Page 18: 2 1. variables & data types

18

The Data Type char

[Ex]

char c;

c= ‘A’+5;

printf(“%c %d\n”, c, c);

[Ex]

c = ‘A’;

c++;

printf(“%c %d\n”, c, c);

[Ex]

for( c = ‘A’; c <= ‘Z’; c++ )

printf(“%c\t”,c);

F 70

B 66

A B C D E … Z

Page 19: 2 1. variables & data types

19

The Data Type char

Nonprinting and hard-to-print characters

Name of character Written in C Integer value

alert

backslash

backspace

carriage return

double quote

formfeed

horizontal tab

newline

null character

single quote

vertical tab

\a

\\

\b

\r

\”

\f

\t

\n

\0

\’

\v

7

92

8

13

34

12

9

10

0

39

11

Escape sequence (특수문자 표현법)

Page 20: 2 1. variables & data types

20

The Floating Types

float, double, long double

– 실수 형의 data 저장

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

– Exponential Notation

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

Page 21: 2 1. variables & data types

21

The Floating Types

float

– 4bytes 의 메모리 할당

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

– 값의 범위 : 10-38 ~ 1038

double

– 8bytes 의 메모리 할당

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

– 값의 범위 : 10-308 ~ 10308

[Ex] double a = 123.45123451234512345;

[Ex] float a = 123.451234;

Page 22: 2 1. variables & data types

The Floating Types

float형 데이터 연산

– 유효 숫자를 7자리까지만 표현. (그러나 그것도 근사값이다.)

22

0.1234567 + 0.00000008 == ?

12345670.0 + 8 == ?

123.4567 + 100000 == ?

Page 23: 2 1. variables & data types

23

Floating Constants

Float Constants :

– Decimal point로 표현

– Exponent로 표현

[Ex] 57.0 /* Decimal point로 표현 */

5.70E1 /*Exponent로 표현 */ .57e+02 570.e-01

Page 24: 2 1. variables & data types

24

Floating Constants

Example

#include <stdio.h> int main(void) { float f=57.0, g=5.70E1, h=.57e+02, i=570e-01; printf( “%.1f %.1f %.1f %.1f\n”, f, g, h, i ); return 0; }

57.0 57.0 57.0 57.0

#include <stdio.h> int main(void) { float f=57.0, g=57.0, h=57.0; printf( “%.1f %.1e %.1E\n”, f, g, h ); return 0; }

57.0 5.7e+001 5.7E+001

Page 25: 2 1. variables & data types

Data Types: 다른 Type 사이의 연산

반올림, 버림 비교

25

int n1, n2;

float f = 1.2;

n1 = f + 0.5;

n2 = f;

float f = 1.23456789;

if( f == 1.23456789 )

printf( “Yes\n” );

else

printf( “No\n” );

Page 26: 2 1. variables & data types

Data Types: 다른 Type 사이의 연산

int, float 사이의 연산

– int형 값과 int형 값의 사칙연산의 결과는 int형이다.

– float형 값과 float형 값의 사칙연산의 결과는 float형이다.

– int형 값과 float형 값의 사칙연산의 결과는 float형이다.

– 두 type 사이의 비교연산은 당신 생각대로 이루어진다.

26

2 + 1 == ?

2 * 1 == ?

3 / 2 == ?

3 % 2 == ?

2.0 + 1.0 == ?

2.0 * 1.0 == ?

3.0 / 2.0 == ?

3.0 % 2.5 == ?

2 + 1.0 == ?

2.0 * 1 == ?

3 / 2.0 == ?

3 % 2.0 == ?

2 < 1 ?

2.0 > 1 ?

2.0 <= 1.0 ?

Page 27: 2 1. variables & data types

27

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