Download C Programming: Data Types, Operators, Statements, Loops, Functions, Recursion, I/O and more Lecture notes Data Structures and Algorithms in PDF only on Docsity! Lecture 01 C programming • C programming 구조 • 연산자 (산술, 관계, 논리, 대입) Prof. DaeEun Kim C language Data type: numbers, characters, Boolean Statements Assignment Conditional statements Loop statements Function statements Input / output 2 Prof. DaeEun Kim Loop statement 1 Repeat a set of statements in a loop (often with the exit condition) while, for, do-while while format while (condition) do S Infinite loop while (1) do S Scond false true 5 Prof. DaeEun Kim Loop statement 2 for format for (initialization; condition; increment) do S equivalent while initialization while (condition) do { S increment; } Infinite loop for ( ; ; ) do S 6 Prof. DaeEun Kim Loop statement 3 do-while format do S while (condition); S is executed at least once Other loop statements goto statement : unconditional branch exit : move the control to the statement just after loop S cond true false 7 Prof. DaeEun Kim Input / output Input function : scanf (argument_list); getchar(); read(); gets() Output function : printf (argument_list); putchar(); write() 10 _ Sizeof example
fs sizeof.c +#/
/? Visual Cee FEE FAS MAB a7] Be
#include <stdio.h>
The size of some fundamental types is computed.
int maing€void)
{ BR
. . BR
printf("The size of some Fundamental types is computed. tintin”), A 7
. " unsigned char: 1 byte
printte chart 23d byte #n", sizeof(char)): Bee re
print signed char! #3d byte #n", sizeof(signed char)): ; - ane
. ” Wier tt Med Pod
printf(” unsigned char: aad byte #n", sizeof(unsigned char); - 4 bytes
printte short: 23d bytes#n”, sizeof(shart}): A ‘i ane
. ” unsigned in Besa os
printf( "unsigned short: 23d bytes#n”, sizeof(unsigned short)) eee in ree
printhe” int: 28d bytes#n", sizeotCint )}: unsigned re 4 nee
printf(” unsigned int: 23d bytestin", sizeof(unsigned)): and 4 nee
printhe long: #8d bytes#in", sizeof(long)): i nee
printf(" unsigned long: 23d bytes#n", sizeof(unsigned long); 8 nee
printfe float23d bytes#n”, sizeof(float)} ieee
printhe” double: 23d bytes#n", sizeot(double}): as
printf(" long double: 23d bytestin", sizeof(long double)).
return }
}
_ float, double
!
#include <stdio.h>
Jint main(void)
i
float f = @.123456789; // float type variable can express up to 6 decimal places
double d = @.123456789; // double type variable can express up to 15 decimal places
printf ("output percentage -> %% \n");
printf("output Double quotes -> \" \n");
printf("output backslash -> \\ \n\n ")3
printf("float :%3d bytes\n", sizeof(f));
printf("double :%3d bytes\n\n", sizeof(double));
printf("f > %.16f \n", f);
printf("d > %.16@f \n\n", d)3
return @;
12
Prof. DaeEun Kim Example #include <stdio.h> main() { int a = 21; int b = 10; int c ; c = a + b; printf("Line 1 - Value of c is %d\n", c ); c = a - b; printf("Line 2 - Value of c is %d\n", c ); c = a * b; printf("Line 3 - Value of c is %d\n", c ); c = a / b; printf("Line 4 - Value of c is %d\n", c ); c = a % b; printf("Line 5 - Value of c is %d\n", c ); c = a++; printf("Line 6 - Value of c is %d\n", c ); c = a--; printf("Line 7 - Value of c is %d\n", c ); } Line 1 – Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22 15 Prof. DaeEun Kim 관계 연산자는 2개의 피연산자 크기를 비교하기 위한 연산자 두 항의 값을 비교하는데 사용 두 개의 문자로 구성되는 관계연산자 기호 사이에는 공백문자 가 없이 연결되어야 하며, 두 개의 문자 순서도 주의 해야 함. 관계 연산자가 포함된 수식의 결과 1(참을 의미)이 아니면 0(거짓을 의미) 관계 연산자 16 Prof. DaeEun Kim &&(and), ||(or), !(not) 논리 연산자는 두 개 또는 하나의 논리값을 0이나 1의 논리값으로 평가하기 위한 연산자 C 언어는 참과 거짓이라는 상수는 없으며, 단순히 0을 거짓으로, 0이 아닌 값을 참으로 간주 정수 만을 이용하는 것은 아니고 실수나 다른 유형의 자료 값도 이용이 가능 다만 평가의 결과는 반드시 0이거나 1 논리 연산자 17 Prof. DaeEun Kim #include <stdio.h> int main() { printf("12345678901234567890\n"); printf("%10c\n", 'A'); printf("%10d\n", 128); printf("%10lf\n", 3.1415926); printf("%10le\n", 3.1415926); printf("%10.3lf\n", 3.1415926); return 0; } 12345678901234567890 A 128 3.141593 3.141593e+000 3.142 printf 20 35 Al0] =A Al
E OO Fieldwidth.c
» OCS Ze S30| tee
= B+ printf()2) Mz A
oF
Si HSAs Daag
redasta
Be lia bs)
4.234088
Pern
2348
123456787812345
4.234088. +808
Beene leo)
Be ee a)
Beene leo)
1123456789012345
iT 7
j a)
Te:
a:
[faa nn ne
Fieldwidth.c
#include <stdio.h>
int main¢void})
{
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
printt(”
return O}
S+ BA(d, a. x): tet estin™, 12945"),
eeode 156 BS wtitiesdwn", 15);
eeooR 156 BS wtitizsown", 15);
ERoxE 156 BS weitieosin", 15);
BR-#OxE 15B BS tet le-oxtintin", 15),
BeAA
FSALt BSF): Wthtlastin”, “12s4587e90" );
ERIOFE 4.2045 BS tet Z1OFWN, 4.234)
BRIO. 4F& 4.2348 BS WL lelO. afin”, 4,234);
SR-10.4f2 4.2945 BS Ott e-10. dfn", 4.294);
FSLt B3(e) Wthtlastin”, “1224567e9012945" );
Belse@ 4.2045 BS Wtetlelbetin", 4.234)
B15. 4e@ 4.2348 BS Wt le15.4etin”, 4,234);
Be+19.4e@ 4.2945 BS Ott le+19.detin", 4,294);
Be-19.4de@ 4.2945 BS Ott le-15.detintn", 4.294);
Gate), EME B43): “12d456709012345" );
Be-1ocE A B BS Wet le-1octin™, A)
Belsse WHE Ses BS wtletsstnin’,
Wtlestin’,
“HEELS 9:
21
float, double
]
2
a
4
5
6
2
a
4
id
11
12
13
i4
16
16
1?
18
ig
Si#include <stdio.h> /#/ printtoje| AS
Md
ao}
ra
a
a
ral
ne}
iH
os
Sint maingvoid) // main he AlSt
{
float f = 0.123456789; /¢% Floate HW
SS 248 018+ Gal Apel aeA| BA IPS
double d = O,129456789; // double HE 248 O|
St 150 Abe MPA] BA tS
printh( "Hae BS = ze tn"):
printh("SUSE BS = #° Hn
printh("S Se] SS > tH Wniin” )
printh("float '#3d bytestin", sizeof(fi):
Printf( "double : 23d bytestintin”, sizeof(double}}:
printhe"f > 2.10F Wn", Fy
Printf("d = 2.10f #niin",dy
return 0:
Caer
8 bytes
oe ea le
cee web
_ int, char
{) #include <stdio.h>
1
2
3 feint maint)
4 {
5 int over = 2147483047; // UD AO] 2°31 - 1
5 unsigned okay = 2147483547;
7
a
int overl, over2:
unsigned okayl, okay2:
printf("char : 3d bytes , ad byte tn", sizeof(char), sizeof A}
printf("int : #3d bytes , ¥8d bytestintin", sizeof( int), sizeaf(over}):
J AMR overt = over +1 2} S2
m
overt = overte: f/B
okay! = okay++:
printf("over!2| 32h
printf(“okay12| 22}
-> E15d Yn", overt):
-3 E1Sd @intin", okayt):
nr rir
over? = over + 10:
okay2 = okay + 10;
print#(“over22] B2}= -> £150 tn“, over2}:
printt(“okay22] BBb= -> £150 infin”, okay2);
printf("ZAb fc2| OFA?| BS Zt -> fdtin", a, ay
printf("ZAb fc2| OFA?| BS Zt -> fdtin", A, AY
printt("= OFA?| BS ete] ab a’ - A! Xd fin”, ‘a
printf("A + 2 -> &ctin”, A+2);
printf("A + 32 -> &clin", Ata2):
printt("A - 82 -> Sctirtin”, 4-32):
return 0:
25
: printf, scanf (character)
[ | G#include <stdio.h> // printfoi2] ASS Bet ag mY Be
2
3 Bint maintvoid) #/ main Se Alat
aif
5 char co:
6 char smal l=32;
7
8 printhe "At obL} HEA BS Sh
g scanf( "fc", &ey
10 printh( "HE At ce ASE ofA] BE SHS BHA Hn", ch
in printf("-> fc, ce, fc, fc, fctin", c, ctl, c+2, c+3, c+4ys
12 printhC"H Est tc 2TAE SHOP ectin", c, c+smally
18
14 return O:
15 }
Prof. DaeEun Kim ++ operation #include <stdio.h> int main() { int a=0, b=0; ++a; b++; printf("a=%d, b=%d\n", a, b); b=a++; printf("a=%d, b=%d\n", a, b); b=++a; printf("a=%d, b=%d\n", a, b); printf("a=%d, b=%d\n", a++, b++); printf("a=%d, b=%d\n", a, b); return 0; } 27