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
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
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
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