SlideShare una empresa de Scribd logo
1 de 27
Descargar para leer sin conexión
Variables & Fundamental Data Types
Structure of C program
 Start with #include <..>
 All statements locate
between “int main(void) {“
and “}”

#include <stdio.h>
int main(void)
{
int x ;

scanf( “%d”, &x ) ;

 All statements end with “;”
 Case sensitive
– “Printf” and “printf” are not the
same

printf( “%dn”, x*x ) ;

}

return 0;

2
Structure of C program
#include <stdio.h>
int main(void)
{

int x ;

Variable declaration
Input

scanf( “%d”, &x ) ;
printf( “%dn”, x*x ) ;
}

Output

return 0;

3
Variables
 Variables (변수)
– 프로그램 실행 중에 값을 저장하기 위한 기억장소.
– 변수는 사용되기 전에 선언되어야 함.
– 프로그램 실행 중 변수는 메모리에 생성됨.

#include <stdio.h>
int main(void)
{
int inches, feet, fathoms;
…

…
inches
feet

fathoms
…

}

4
Variables
 Variables Naming Rule
– 영문자 또는 _로 시작.
– 영문자, 숫자, _(underbar) 로 구성.
[Ex]
사용 가능한 변수명 : times10, get_next_char, _done
사용 불가능한 변수명 : 10times, get-next-char, int

– maximum 255자 까지 가능.
– 예약어(Reserved workds)는 변수이름으로 사용할 수 없음.

5
Variables
 Reserved Words
– C언어의 일부로 의미와 사용처가 이미 결정된 단어들.
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

6
Variables
 그런데, 변수 앞에 있는 int는 무엇인가??
#include <stdio.h>

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

int main(void)
{
int inches, feet, fathoms;

…
}

7
The Fundamental Data Types
 C에서 제공하는 Data Types
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

– ‘signed’ keyword는 생략 가능
• int 와 signed int, long과 signed long 등은 각각 같은 의미

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

8
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)
9
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)

10
The Integral Types
 양수와 음수의 비트 표현 : 2 bytes int의 경우
unsinged int의 경우

int의 경우
0000
0000
0000
0000
…
0111
0111
1000
1000
1000
…
1111
1111
1111

0000
0000
0000
0000

0000
0000
0000
0000

0000
0001
0010
0011

->
->
->
->

0
1
2
3

1111
1111
0000
0000
0000

1111
1111
0000
0000
0000

1110
1111
0000
0001
0010

->
->
->
->
->

215-2
215-1
-215
-215+1
-215+2

1111 1111 1101 -> -3
1111 1111 1110 -> -2
1111 1111 1111 -> -1

0000
0000
0000
0000
…
0111
0111
1000
1000
1000
…
1111
1111
1111

0000
0000
0000
0000

0000
0000
0000
0000

0000
0001
0010
0011

->
->
->
->

0
1
2
3

1111
1111
0000
0000
0000

1111
1111
0000
0000
0000

1110
1111
0000
0001
0010

->
->
->
->
->

215-2
215-1
215
215+1
215+2

1111 1111 1101 -> 216-3
1111 1111 1110 -> 216-2
1111 1111 1111 -> 216-1
11
The Integral Types
 예제 : 4 byte machine
int i = 2147483645, j ;
for( j = 0 ; j < 5 ; j++ )
printf( “%dn”, i + j ) ;

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

2147483645
2147483646
2147483647
-2147483648
-2147483647
2147483645
2147483646
2147483647
2147483648
2147483649

12
The Integral Types
 예제 : 4 byte machine
int i = -1 ;
unsigned u = -1 ;

-1 4294967295
-1 -1
4294967295 4294967295

printf( “%d %un”, i, u ) ;
printf( “%d %dn”, i, u ) ;
printf( “%u %un”, i, u ) ;

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

13
Integer Constants
 Integer Constants :
– C에서 정수형은 Decimal, Octal, Hexadecimal로 표현된다.
[Ex]

17
017
0x17
-17

/* decimal integer constant
*/
/* octal integer constant : 17(8) = 15
*/
/* hexadecimal integer constant 17(16)= 23 */
/* negative decimal integer constant
*/

14
Integer Constants
 Example
#include <stdio.h>
int main(void) {
int i = 17, j = 017, k =0x17;
printf( “%d %d %dn”, i, j, k );
return 0;
}
#include <stdio.h>
int main(void) {
int i = 15;
printf( “%d %o %x %Xn”, i, i, i, i );
return 0;
}

17 15 23

15 17 f F

15
The Data Type char
 Char type
– 8bit의 ASCII code로 표현
– 총 256 개의 char 표현 가능
– 문자 또는 작은 수의 int로 표현
[Ex]
printf(“%c”, ‘a’ );
printf(“%c%c%c”, ‘A’, ‘ B’, ‘C’ );
printf(“%c”, 97 );
printf(“%c”, ‘a’+1 );

/*
/*
/*
/*

printf(“%d”, ‘a’ );

/* 97 is printed */

a is printed */
ABC is printed */
a is printed */
b is printed */

16
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 */

17
The Data Type char
[Ex]
char c;
c= ‘A’+5;
printf(“%c %dn”, c, c);
[Ex]
c = ‘A’;
c++;
printf(“%c %dn”, c, c);
[Ex]
for( c = ‘A’; c <= ‘Z’; c++ )
printf(“%ct”,c);

F 70

B 66

A B C D E…Z

18
The Data Type char
 Escape sequence (특수문자 표현법)
Nonprinting and hard-to-print characters
Name of character

alert
backslash
backspace
carriage return
double quote
formfeed
horizontal tab
newline
null character
single quote
vertical tab

Written in C

Integer value

a

b
r
”
f
t
n
0
’
v

7
92
8
13
34
12
9
10
0
39
11
19
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

20
The Floating Types
 float
– 4bytes 의 메모리 할당
– 유효숫자 6자리의 정확도
– 값의 범위 : 10-38 ~ 1038
[Ex] float a = 123.451234;

 double
– 8bytes 의 메모리 할당
– 유효숫자 15자리의 정확도
– 값의 범위 : 10-308 ~ 10308
[Ex] double a = 123.45123451234512345;
21
The Floating Types
 float형 데이터 연산
– 유효 숫자를 7자리까지만 표현. (그러나 그것도 근사값이다.)
0.1234567 + 0.00000008 == ?
12345670.0 + 8 == ?
123.4567 + 100000 == ?

22
Floating Constants
 Float Constants :
– Decimal point로 표현
– Exponent로 표현
[Ex]

57.0

/* Decimal point로 표현

*/

5.70E1
.57e+02
570.e-01

/*Exponent로 표현

*/

23
Floating Constants
 Example

57.0 57.0 57.0 57.0

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

57.0 5.7e+001 5.7E+001

24
Data Types: 다른 Type 사이의 연산
 반올림, 버림
int n1, n2;

 비교
float f = 1.23456789;

float f = 1.2;
if( f == 1.23456789 )

n1 = f + 0.5;
n2 = f;

printf( “Yesn” );
else
printf( “Non” );

25
Data Types: 다른 Type 사이의 연산
 int, float 사이의 연산
– int형 값과 int형 값의 사칙연산의 결과는 int형이다.
– float형 값과 float형 값의 사칙연산의 결과는 float형이다.
– int형 값과 float형 값의 사칙연산의 결과는 float형이다.
– 두 type 사이의 비교연산은 당신 생각대로 이루어진다.
2 + 1 == ?

2.0 + 1.0 == ?

2 + 1.0 == ?

2<1

?

2 * 1 == ?

2.0 * 1.0 == ?

2.0 * 1 == ?

2.0 > 1 ?

3 / 2 == ?

3.0 / 2.0 == ?

3 / 2.0 == ?

2.0 <= 1.0 ?

3 % 2 == ?

3.0 % 2.5 == ?

3 % 2.0 == ?

26
Casts
 Casts
– expression에서 operand의 type을 convert
– (type명)expression
[Ex1]

int a=3, b=2;
double c = a / b;
printf(“c=%fn”, c);

[Ex2]

c=1.000000

int a=3, b=2;
double c = (double) a / b;
printf(“c=%fn”, c);

c=1.500000
27

Más contenido relacionado

La actualidad más candente

파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차Han Sung Kim
 
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Circulus
 
Modern effective c++ 항목 3
Modern effective c++ 항목 3Modern effective c++ 항목 3
Modern effective c++ 항목 3ssuser7c5a40
 
2. c언어의 기본
2. c언어의 기본2. c언어의 기본
2. c언어의 기본SeonMan Kim
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기Jongwook Choi
 
TestBCD2018-2(answer)
TestBCD2018-2(answer)TestBCD2018-2(answer)
TestBCD2018-2(answer)Yong Heui Cho
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3Chris Ohk
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2Chris Ohk
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Circulus
 
C++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threadsC++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threadsSeok-joon Yun
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3guestc0587d1
 
C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부Gwangwhi Mah
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple OverviewKim Hunmin
 

La actualidad más candente (20)

BOJ4743
BOJ4743BOJ4743
BOJ4743
 
파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차
 
함수
함수함수
함수
 
(닷넷, C#기초교육)C#선택적인수, 명명된 인수
(닷넷, C#기초교육)C#선택적인수, 명명된 인수(닷넷, C#기초교육)C#선택적인수, 명명된 인수
(닷넷, C#기초교육)C#선택적인수, 명명된 인수
 
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리
 
Modern effective c++ 항목 3
Modern effective c++ 항목 3Modern effective c++ 항목 3
Modern effective c++ 항목 3
 
2. c언어의 기본
2. c언어의 기본2. c언어의 기본
2. c언어의 기본
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기
 
TestBCD2018-2(answer)
TestBCD2018-2(answer)TestBCD2018-2(answer)
TestBCD2018-2(answer)
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2
 
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 6 - 함수, 스코프, 클로저
 
C++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threadsC++ Concurrency in Action 9-2 Interrupting threads
C++ Concurrency in Action 9-2 Interrupting threads
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3
 
C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
Haskell study 9
Haskell study 9Haskell study 9
Haskell study 9
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple Overview
 

Similar a 2 1. variables & data types

불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 명신 김
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차Yeonah Ki
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자웅식 전
 
빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)SeongHyun Ahn
 
C수업자료
C수업자료C수업자료
C수업자료koominsu
 
C수업자료
C수업자료C수업자료
C수업자료koominsu
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard LibraryDongMin Choi
 
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성Lee Sang-Ho
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1Chris Ohk
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산S.O.P.T - Shout Our Passion Together
 
Nexon Developers Conference 2017 Functional Programming for better code - Mod...
Nexon Developers Conference 2017 Functional Programming for better code - Mod...Nexon Developers Conference 2017 Functional Programming for better code - Mod...
Nexon Developers Conference 2017 Functional Programming for better code - Mod...Isaac Jeon
 

Similar a 2 1. variables & data types (20)

불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차
 
06장 함수
06장 함수06장 함수
06장 함수
 
6 function
6 function6 function
6 function
 
java_2장.pptx
java_2장.pptxjava_2장.pptx
java_2장.pptx
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자
 
C review
C  reviewC  review
C review
 
빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)빠르게 활용하는 파이썬3 스터디(ch1~4)
빠르게 활용하는 파이썬3 스터디(ch1~4)
 
자료구조05
자료구조05자료구조05
자료구조05
 
자료구조05
자료구조05자료구조05
자료구조05
 
C수업자료
C수업자료C수업자료
C수업자료
 
C수업자료
C수업자료C수업자료
C수업자료
 
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
[C++ Korea 2nd Seminar] Ranges for The Cpp Standard Library
 
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
 
Changes in c++0x
Changes in c++0xChanges in c++0x
Changes in c++0x
 
HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
 
2013 C++ Study For Students #1
2013 C++ Study For Students #12013 C++ Study For Students #1
2013 C++ Study For Students #1
 
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
[SOPT] 데이터 구조 및 알고리즘 스터디 - #02 : 스택, 큐, 수식 연산
 
Nexon Developers Conference 2017 Functional Programming for better code - Mod...
Nexon Developers Conference 2017 Functional Programming for better code - Mod...Nexon Developers Conference 2017 Functional Programming for better code - Mod...
Nexon Developers Conference 2017 Functional Programming for better code - Mod...
 

Más de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 

Más de 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 

2 1. variables & data types

  • 2. Structure of C program  Start with #include <..>  All statements locate between “int main(void) {“ and “}” #include <stdio.h> int main(void) { int x ; scanf( “%d”, &x ) ;  All statements end with “;”  Case sensitive – “Printf” and “printf” are not the same printf( “%dn”, x*x ) ; } return 0; 2
  • 3. Structure of C program #include <stdio.h> int main(void) { int x ; Variable declaration Input scanf( “%d”, &x ) ; printf( “%dn”, x*x ) ; } Output return 0; 3
  • 4. Variables  Variables (변수) – 프로그램 실행 중에 값을 저장하기 위한 기억장소. – 변수는 사용되기 전에 선언되어야 함. – 프로그램 실행 중 변수는 메모리에 생성됨. #include <stdio.h> int main(void) { int inches, feet, fathoms; … … inches feet fathoms … } 4
  • 5. Variables  Variables Naming Rule – 영문자 또는 _로 시작. – 영문자, 숫자, _(underbar) 로 구성. [Ex] 사용 가능한 변수명 : times10, get_next_char, _done 사용 불가능한 변수명 : 10times, get-next-char, int – maximum 255자 까지 가능. – 예약어(Reserved workds)는 변수이름으로 사용할 수 없음. 5
  • 6. Variables  Reserved Words – C언어의 일부로 의미와 사용처가 이미 결정된 단어들. 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 6
  • 7. Variables  그런데, 변수 앞에 있는 int는 무엇인가?? #include <stdio.h> 변수가 저장할 수 있는 값을 종류를 의미함 int main(void) { int inches, feet, fathoms; … } 7
  • 8. The Fundamental Data Types  C에서 제공하는 Data Types 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 – ‘signed’ keyword는 생략 가능 • int 와 signed int, long과 signed long 등은 각각 같은 의미 – short int, long int, unsigned int에서 int의 생략 가능 • short, long, unsigned로 사용 8
  • 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) 9
  • 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) 10
  • 11. The Integral Types  양수와 음수의 비트 표현 : 2 bytes int의 경우 unsinged int의 경우 int의 경우 0000 0000 0000 0000 … 0111 0111 1000 1000 1000 … 1111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 0010 0011 -> -> -> -> 0 1 2 3 1111 1111 0000 0000 0000 1111 1111 0000 0000 0000 1110 1111 0000 0001 0010 -> -> -> -> -> 215-2 215-1 -215 -215+1 -215+2 1111 1111 1101 -> -3 1111 1111 1110 -> -2 1111 1111 1111 -> -1 0000 0000 0000 0000 … 0111 0111 1000 1000 1000 … 1111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 0000 0001 0010 0011 -> -> -> -> 0 1 2 3 1111 1111 0000 0000 0000 1111 1111 0000 0000 0000 1110 1111 0000 0001 0010 -> -> -> -> -> 215-2 215-1 215 215+1 215+2 1111 1111 1101 -> 216-3 1111 1111 1110 -> 216-2 1111 1111 1111 -> 216-1 11
  • 12. The Integral Types  예제 : 4 byte machine int i = 2147483645, j ; for( j = 0 ; j < 5 ; j++ ) printf( “%dn”, i + j ) ; unsigned int i = 2147483645, j ; for( j = 0 ; j < 5 ; j++ ) printf( “%un”, i + j ) ; 2147483645 2147483646 2147483647 -2147483648 -2147483647 2147483645 2147483646 2147483647 2147483648 2147483649 12
  • 13. The Integral Types  예제 : 4 byte machine int i = -1 ; unsigned u = -1 ; -1 4294967295 -1 -1 4294967295 4294967295 printf( “%d %un”, i, u ) ; printf( “%d %dn”, i, u ) ; printf( “%u %un”, i, u ) ; int 변수에 %u를 사용하면, int를 unsigned int 취급한다. unsigned int 변수에 %d를 사용하면, unsigned int를 int 취급한다. 13
  • 14. Integer Constants  Integer Constants : – C에서 정수형은 Decimal, Octal, Hexadecimal로 표현된다. [Ex] 17 017 0x17 -17 /* decimal integer constant */ /* octal integer constant : 17(8) = 15 */ /* hexadecimal integer constant 17(16)= 23 */ /* negative decimal integer constant */ 14
  • 15. Integer Constants  Example #include <stdio.h> int main(void) { int i = 17, j = 017, k =0x17; printf( “%d %d %dn”, i, j, k ); return 0; } #include <stdio.h> int main(void) { int i = 15; printf( “%d %o %x %Xn”, i, i, i, i ); return 0; } 17 15 23 15 17 f F 15
  • 16. The Data Type char  Char type – 8bit의 ASCII code로 표현 – 총 256 개의 char 표현 가능 – 문자 또는 작은 수의 int로 표현 [Ex] printf(“%c”, ‘a’ ); printf(“%c%c%c”, ‘A’, ‘ B’, ‘C’ ); printf(“%c”, 97 ); printf(“%c”, ‘a’+1 ); /* /* /* /* printf(“%d”, ‘a’ ); /* 97 is printed */ a is printed */ ABC is printed */ a is printed */ b is printed */ 16
  • 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 */ 17
  • 18. The Data Type char [Ex] char c; c= ‘A’+5; printf(“%c %dn”, c, c); [Ex] c = ‘A’; c++; printf(“%c %dn”, c, c); [Ex] for( c = ‘A’; c <= ‘Z’; c++ ) printf(“%ct”,c); F 70 B 66 A B C D E…Z 18
  • 19. The Data Type char  Escape sequence (특수문자 표현법) Nonprinting and hard-to-print characters Name of character alert backslash backspace carriage return double quote formfeed horizontal tab newline null character single quote vertical tab Written in C Integer value a b r ” f t n 0 ’ v 7 92 8 13 34 12 9 10 0 39 11 19
  • 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 20
  • 21. The Floating Types  float – 4bytes 의 메모리 할당 – 유효숫자 6자리의 정확도 – 값의 범위 : 10-38 ~ 1038 [Ex] float a = 123.451234;  double – 8bytes 의 메모리 할당 – 유효숫자 15자리의 정확도 – 값의 범위 : 10-308 ~ 10308 [Ex] double a = 123.45123451234512345; 21
  • 22. The Floating Types  float형 데이터 연산 – 유효 숫자를 7자리까지만 표현. (그러나 그것도 근사값이다.) 0.1234567 + 0.00000008 == ? 12345670.0 + 8 == ? 123.4567 + 100000 == ? 22
  • 23. Floating Constants  Float Constants : – Decimal point로 표현 – Exponent로 표현 [Ex] 57.0 /* Decimal point로 표현 */ 5.70E1 .57e+02 570.e-01 /*Exponent로 표현 */ 23
  • 24. Floating Constants  Example 57.0 57.0 57.0 57.0 #include <stdio.h> int main(void) { float f=57.0, g=5.70E1, h=.57e+02, i=570e-01; printf( “%.1f %.1f %.1f %.1fn”, f, g, h, i ); return 0; } #include <stdio.h> int main(void) { float f=57.0, g=57.0, h=57.0; printf( “%.1f %.1e %.1En”, f, g, h ); return 0; } 57.0 5.7e+001 5.7E+001 24
  • 25. Data Types: 다른 Type 사이의 연산  반올림, 버림 int n1, n2;  비교 float f = 1.23456789; float f = 1.2; if( f == 1.23456789 ) n1 = f + 0.5; n2 = f; printf( “Yesn” ); else printf( “Non” ); 25
  • 26. Data Types: 다른 Type 사이의 연산  int, float 사이의 연산 – int형 값과 int형 값의 사칙연산의 결과는 int형이다. – float형 값과 float형 값의 사칙연산의 결과는 float형이다. – int형 값과 float형 값의 사칙연산의 결과는 float형이다. – 두 type 사이의 비교연산은 당신 생각대로 이루어진다. 2 + 1 == ? 2.0 + 1.0 == ? 2 + 1.0 == ? 2<1 ? 2 * 1 == ? 2.0 * 1.0 == ? 2.0 * 1 == ? 2.0 > 1 ? 3 / 2 == ? 3.0 / 2.0 == ? 3 / 2.0 == ? 2.0 <= 1.0 ? 3 % 2 == ? 3.0 % 2.5 == ? 3 % 2.0 == ? 26
  • 27. Casts  Casts – expression에서 operand의 type을 convert – (type명)expression [Ex1] int a=3, b=2; double c = a / b; printf(“c=%fn”, c); [Ex2] c=1.000000 int a=3, b=2; double c = (double) a / b; printf(“c=%fn”, c); c=1.500000 27