Computer Programming Part 3
Computer Programming Part 3
1
Data is
level
exp
points
recharge
time
reset time
score of rank 1
level of rank 1
기초컴퓨터프로그래밍 2
Data type
Human data type/ computer
Computers process numbers (data). data type
There are specific types of numbers a computer can handle.
Integer
– most commonly used data type
정수형의 범위
size in size in
data type range of representable numbers
byte bit
char 1 8 -128 ~ 127
short 2 16 -32,768 ~ 32,767
int 4 32 -2,147,483,648 ~ 2,147,483,647
long 4 32 -2,147,483,648 ~ 2,147,483,647
Approximately -9.22 quintillion to
long long 8 64
9.22 quintillion
기초컴퓨터프로그래밍 4
Integer
char type
– smallest number largest number
8 bit = 1 byte
0 0 0 0 0 0 0 0 =0 1 1 1 1 1 1 1 1 =255
int type
– Historically, the int type was 16 bits, but now it is 32 bits.
char type
– used to store characters, so it is also referred to as a character
type.
기초컴퓨터프로그래밍 6
Quiz)
Answer
– What is the data type of the number 120?
– What data type is suitable for storing the number 120?
– What is the best data type to store/process 120 + 120?
– What is the difference between storing 120 as char and long
long?
– What data type is best for storing numbers between 0 and
500,000?
– What is the best data type to store a number between 0 and
300?
– Is char type an integer type or a character type?
– What is the most commonly used integer type?
– What is a 64-bit computer?
기초컴퓨터프로그래밍 7
Computers represent
Floating Point Types numbers using 0s
and 1s
Significant
data type size (byte) range
digits
float 4 7 ±10-38 ~ 1038
double 8 15 ±10-308 ~ 10308
long double 8 15 ±10-308 ~ 10308
기초컴퓨터프로그래밍 8
floating point
significant digits
– Due to the limitations of the computer's storage method
(binary), digits beyond the reliable significant digits cannot be
trusted.
Definition
– Computers process numbers (integers and floating points).
– Characters are represented using numbers (e.g., ASCII codes).
– char type is used to store characters efficiently(int is not good?).
ASCII Code
– maps numbers from 0 to 127 to corresponding characters for
storing text
– ASCII Code is one method of interpreting numbers as characters.
– In any case, computers only store numbers
ASCII Code Examples
– Digits (0-9): 48 to 57
– Uppercase A, lowercase a: 65, 97 Korean char?
– Space, newline: 32, 10 Japanese char?
Chinese char?
기초컴퓨터프로그래밍 10
char
기초컴퓨터프로그래밍 11
unsigned
Data types that are not provided by default (e.g., complex numbers)
must be created and used by the developer.
기초컴퓨터프로그래밍 12
Quiz)
기초컴퓨터프로그래밍 13
Data type in C
char
char
int / short
int / short
char char
char int
기초컴퓨터프로그래밍 14
C 언어 자료형의 이해
메모리는 소중하다.
– 정수(int) 1개를 저장할 때에는 4 바이트를 차지하지만,
기초컴퓨터프로그래밍 15
Variables
A variable is used to
store values and perform
Definition calculations.
– A space in memory used to store and manipulate data in a
program.
– In C programs, variables are used by assigning them names.
– Once a value is stored in a variable, it can be retrieved at any time.
– New values can be stored in a variable at any time.
– A variable must have a data type.
– A variable stores values that match its data type.
– A variable must be declared before it is used.
• data type varname
폰노이만 구조 모든 프로그램은 메모리에 탑재되어 실행된다.
메모리
변수 프로그램
기초컴퓨터프로그래밍 16
Variable Declaration
기초컴퓨터프로그래밍 18
example
#include <stdio.h>
int main( )
{
int total, score, value;
total = 100;
score = 90;
value = 10; // 10.1 이라면?
/* 이제 무엇인가 합시다. */
score = total + value;
printf("%d\n", score);
}
기초컴퓨터프로그래밍 19
variable name
기초컴퓨터프로그래밍 20
variable name
기초컴퓨터프로그래밍 21
variable name
https://www.newiki.net/wiki/코딩_스타일
Naming variables with multiple words
– Capitalizing the first letter of each word : AverageScore,
TotalScore
– Adding an underscore between words. : average_score,
total_score
Attaching a data type to a variable
– iTotalScore : variable name clearly indicates that it is an integer
– fAverageScore : floating type
Invalid variable name (keyword, or reserved word)
– Reserved words in C language for special purposes
• auto, break, case, char, const, continue, default, do, double
• else, enum, extern, float, for, goto, if, return, short, sighed,
• sizeof, static, struct, switch, typedef, union, unsigned, void, while 22
기초컴퓨터프로그래밍
Variable initialization
int value;
int value = 10;
value = 10;
기초컴퓨터프로그래밍 23
Variable initialization
#include <stdio.h>
garbage values.
int main( )
{ main memory
int number = 10, value; 10 -873213
number value
기초컴퓨터프로그래밍 24
Variable initialization
int num;
float cel;
num = 3.14;
cel = 3.0;
cel = 4;
기초컴퓨터프로그래밍 25
Variable initialization
name = 50;
value = name; // Before using the garbage value
// in the 'value' variable,
//it is filled with another value.
warning or error
int value, name;
value = name;
printf(“%d\n”, total);
// Display the value of the total variable on the screen.
기초컴퓨터프로그래밍 28
sizeof operator
score = 99;
기초컴퓨터프로그래밍 29
constant
value in a program
example
– integer constant 10, 384
score = 10;
6 2 4 6 4 C A 6 16진수
8진수
기초컴퓨터프로그래밍 32
number notation (integer)
– example:
value = 010; // 8진수 10, 10진수 8을 value에 넣는다.
score = 0x10; // 16진수 10, 10진수 16을 score에 넣는다.
기초컴퓨터프로그래밍 33
Input of exponential data(float)
Limitations of input:
– You cannot input 10100 (big number) in the following way (error).
double fvalue =
10000000000000000000000000000000000000000…………
기초컴퓨터프로그래밍 34
Exercise
기초컴퓨터프로그래밍 35
Overflow & Underflow
Data type in C size (byte) range
char 1 -128~127
short 2 -32768 - 32767
integer int 4 -2,147,483,648 ~ 2,147,483,647
long 4 -2,147,483,648 ~ 2,147,483,647
long long 8 약 -922경 ~ 922경 (이하 자리 생략)
float a, b;
a = 0.1; // warning
b = 0.5; // OK
… … … …
-3 1111……1101 3 0000……0011
-2 1111……1110 2 0000……0010
-1 1111……1111 1 0000……0001
0 0000……0000 0 0000……0000
기초컴퓨터프로그래밍 38
float notation
± 𝟏. 𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇𝒇 ∗ 𝟐𝒆𝒆𝒆𝒆𝒆𝒆𝒆𝒆−𝟏𝟐𝟕
exponent
mentissa
0 e e e e e e e e f f f f f f f f f f f f f f f f f f f f f f f
기초컴퓨터프로그래밍 40