๐ก
๋ฐ๋ฐฐ์จ 5.1 ~ ๋ณธ๋ฌธ
5.1 ๋ฐ๋ณต ๋ฃจํ์์ ์ฒซ๋ง๋จ
#include <stdio.h>
int main()
{
int n = 1; // ๋ณ์ ์ ์ธ, ์ด๊ธฐํ
while (n < 11) // ๋ฐ๋ณต๋ฌธ ์กฐ๊ฑด
{
printf("%d\n", n);
n = n + 1; // ๋ณ์ ๋ณํ๋ ์กฐ๊ฑด
}
return 0;
}
5.2 ๋์ ์ฐ์ฐ์์ ๋ช ๊ฐ์ง ์ฉ์ด๋ค
๊ธฐ๋ณธ ์ฐ์ฐ์๋ค
=, +, -, *, /
์ฐ์ฐ์ operator
ํผ์ฐ์ฐ์ operand
Data object (object) : ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋(์กด์ฌํ๋) ๋ฐ์ดํฐ
L-value (object locator value) : ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐจ์งํ๊ณ ์๋ ํน์ ๋ฐ์ดํฐ ๊ฐ์ฒด(๊ฐ์ฒด)
R-value (value of an expression) : ์์ ๊ฐ๋ฅํ L-value์๊ฒ ๋์ ๋ ์๋ ์์ง๋ง
์๊ธฐ ์์ ์ L-value๊ฐ ๋ ์ ์๋ ๊ฒ๋ค
const int TWO = 2; // TWO๋ ์์ ๋ถ๊ฐ๋ฅํ L-value(์ฌ๊ธฐ์ =๋ ๋์ ์ด ์๋๋ผ ์ด๊ธฐํ)
int a; // a, b, c๋ ์์ ๊ฐ๋ฅํ L-value
int b;
int c;
a = 42; // 42๋ R-value
b = a; // (a+b)๋ R-value(ํ๋ก๊ทธ๋จ์ด ๊ณ์ผํ๋ ์์ ๊ฐ, ๋๋๋ฉด ์ฌ๋ผ์ง)
c = TWO * (a + b);
5.3 ๋ํ๊ธฐ, ๋นผ๊ธฐ, ๋ถํธ ์ฐ์ฐ์๋ค
#include <stdio.h>
int main()
{
prinf("%d\n", 1 + 2);
int income, salary, bonus;
income = salary = bonus = 100; // tripleassignment
salary = 100;
bonus = 30;
income = salary + bonus; // L-value vs R-value
int takenhome, tax;
tax = 20;
takehome = income - tax;
int a, b;
a = -7;
b = -a;
b = +a; // does nothing
1.0f + 2; // float์ ์ ์ฅ๋๋ค
return0;
}
์ดํญ ์ฐ์ฐ์ Binary operator
3 - 2 ๊ฐvalue์ด 1
(ํผ์ฐ์ฐ์ 2๊ฐ)
๋จํญ ์ฐ์ฐ์ Unary operator
-16 ๊ฐvalue์ด -16
(ํผ์ฐ์ฐ์ 1๊ฐ)
๋ณตํฉ
-(12-11) ๊ฐ์ด -1
5.4 ๊ณฑํ๊ธฐ ์ฐ์ฐ์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
double seed_money, target_money, annual_interest;
printf("Input seed money : ");
scanf("%lf", &seed_money);
printf("Input target money : ");
scanf("%lf", &target_money);
printf("Input annual interest (%%) : ");
scanf("%lf", &annual_interest);
double fund = seed_money;
int year_count = 0;
while (fund < target_money)
{
fund = fund + fund * annual_interest / 100;
year_count = year_count + 1;
printf("Year %d, fund %f\n", year_count, fund);
}
printf("It takes %d years\n", year_count);
return 0;
}
5.5 ๋๋๊ธฐ ์ฐ์ฐ์
#include <stdio.h>
int main()
{
printf("Integer divisions\n";)
printf("%d\n", 14 / 7); // 2
printf("%d\n", 7 / 2); // 3
printf("%d\n", 7 / 3); // 2
printf("%d\n", 7 / 4); // 1
printf("%d\n", 8 / 4); // 2
printf("Truncationg toward zero (C99)\n");
printf("%d\n", -7 / 2); // -3
printf("%d\n", -7 / 3); // -2
printf("%d\n", -7 / 4); // -1
printf("%d\n", -8 / 4); // -2
printf("\nFloating divisions\n");
printf("%f\n", 9.0 / 4.0); // 2.250000
printf("%f\n", 9.0 / 4); // 2.250000 Note: 4 is integer
return 0;
}
์ ์๋ผ๋ฆฌ์ ์ฐ์ฐ์ ์ค์๋ฅผ ๋ด์์ ์๊ธฐ ๋๋ฌธ์ ๋ฐ์ฌ๋ฆผ ์์ด ์ ์๋ถ๋ถ๋ง ๋์จ๋ค.
์๋ฃํ์ด ๋ค๋ฅธ ๋๊ฐ์ ์ฐ์ฐ( float / interger)์ integer๋ฅผ double๋ก ๋ฐ๊ฟ์ ์ฐ์ฐํด์ค๋ค.
5.6 ์ฐ์ฐ์ ์ฐ์ ์์์ ํํ์ ํธ๋ฆฌ
ํํ์ ํธ๋ฆฌ
a = 4.0;
b = 5.0;
c = 3.0 + 5.0 * b / a
์ฐ์ฐ์ ์ฐ์ ์์
5.7 ๋๋จธ์ง ์ฐ์ฐ์
int a = 13 % 5; // 3
int b = 90 % 60; // 30
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int seconds = 0, minutes = 0, hours = 0;
printf("Input seconds : ");
scanf("%d", &seconds);
while (seconds >= 0)
{
hours = seconds / 3600;
minutes = (seconds % 3600) / 60;
seconds = (seconds % 3600) % 60;
/* ๋ฐ๋ฐฐ์จ ์ฝ๋
minutes = seconds / 60;
seconds %= 60;
hours = minutes / 60;
minutes %= 60;
*/
printf("%d hours, %d minutes, %d seconds\n", hours, minutes, seconds);
printf("Input seconds : ");
scanf("%d", &seconds);
}
printf("Good bye\n");
return 0;
}
while๋ฌธ ๋ฐ์์ ์ ๋ ฅ์ ๋จผ์ ๋ฐ๊ณ ์์์ ์ถ๋ ฅ์ ํ ํ ์ ๋ ฅ์ ๋ค์ ๋ฐ์ผ๋ฏ๋ก์
์์๊ฐ ์ถ๋ ฅ๋๋ฉด ์๊ฐ์ ์ถ๋ ฅํ์ง ์๊ณ while๋ฌธ์ ๋๊ฐ๊ฒ ๋๋ค.
์์์ ๋๋จธ์ง์ฐ์ฐ
#include <stdio.h>
int main()
{
int div, mod;
div = 11 / 5; // 2
mod = 11 % 5; // 1
printf("div = %d, mod = %d\n", div, mod);
div = 11 / -5; // -2
mod = 11 % -5; // 1
printf("div = %d, mod = %d\n", div, mod);
div = -11 / -5; // 2
mod = -11 % -5; // -1
printf("div = %d, mod = %d\n", div, mod);
div = -11 / 5; // -2
mod = -11 % 5; // -1
printf("div = %d, mod = %d\n", div, mod);
return 0;
}
์์ ์๋ ์๊ฐ ์์๋ฉด ๋๋จธ์ง๋ ์์๋ก ๋์จ๋ค.
5.8 ์ฆ๊ฐ, ๊ฐ์ ์ฐ์ฐ์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
int a = 0;
a++; // a = a + 1 or a += 1;
printf("%d\n", a);
++a; // a = a + a or a += 1;
printf("%d\n", a);
double b = 0;
b++;
printf("%f\n", b);
++b;
printf("%f\n", b);
/* ++ and -- affect modifiable values
int count = 0;
whlie(count < 10) // ++count or count++
{
printf("%d", count); // ++count or count++
}
*/
/*
int i = 1, j = 1;
int i_post, pre_j
i_post = i++;
pre_j = ++j;
printf("%d %d\n", i, j);
printf("%d %d\n", i_post, pre_j);
*/
/*
int i = 3;
int l = 2 * --i;
printf("%d %d\n", i, l);
i = 1;
l = 2 * i--;
printf("%d %d\n", i, l);
*/
// veryhigh precedence
int x, y, z;
x = 3, y = 4;
z = (x + y++) * 5; // (x+y)++ X, x+(y++) O
printf("%d %d %d\n", x, y, z); // 3 5 35
/*
int x = 1, y = 1, z;
z = x * y++; // (x)* (y++), not (x*y)++
// z = (x * y)++; // error
// z = 3++; // erroe
*/
return 0;
}
์ฆ๊ฐ, ๊ฐ์ ์ฐ์ฐ์๋ ์ ์, ํ์์ฐ์ฐ์๋ก ๋๋ ์ ์๋ค.
-๋ด๊ฐ ์ดํดํ ๋ฐ๋ก๋ ์ ์ ์ฐ์ฐ์๋ ๋จผ์ ๊ฐ์ ์ฆ๊ฐ(๊ฐ์)์ํจ ํ์ ํด๋น ๋ฌธ์ฅ์ ์ฒ๋ฆฌํ๊ณ
ํ์ ์ฐ์ฐ์๋ ํด๋น ๋ฌธ์ฅ์ ์ฒ๋ฆฌํ ํ์ ๊ฐ์ ์ฆ๊ฐ(๊ฐ์)์ํจ๋ค.
-์ฆ๊ฐ, ๊ฐ์ ์ฐ์ฐ์๋ ์์ ๊ฐ๋ฅํ L-value์๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค. ์ฆ ์์๋ ๋ฆฌํฐ๋ด์๋ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ.
-ํ ๋ฌธ์ฅ์์ ์ฆ๊ฐ, ๊ฐ์ ์ฐ์ฐ์๋ฅผ ์ฌ๋ฌ๋ฒ ์ฐ๋ ๊ฒ์ ์ข์ง ์๋ค.(์ปดํ์ผ๋ฌ์ ๋ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅผ ์ ์๋ค)
5.9 ํํ์Expressions ๊ณผ ๋ฌธ์ฅStatements
ํํ์ Expressioms
ํํ์ | ๊ฐ |
4 -6 4 + 21 q = 5 * 2 3 + (c = 1 + 2) 2 > 1 2 < 1 a x = ++q % 3 q > 3 a * (b / c + d) / 20 |
4 -6 25 10 6 1 (true) 0 (false) a์ ๊ฐ x์ q์ ๊ฐ์ ์ํด ๊ฒฐ์ q์ ๊ฐ์ ์ํด ๊ฒฐ์ a, b, c, d์ ๊ฐ์์ํด ๊ฒฐ์ |
๋ฌธ์ฅ Statements
int x, y, apples; // declaraion statement
apples = 3 // assignment statement
; // null statement
7;
1 + 2;
x = 4;
++x;
x = 1 + (y = 5) // y = 5 is subexperssion
while (x++ < 10) // while statement (structured statement)
y = x + y;
printf("%d\n", y); // function statement
return 0; // return statement
Side Effects and Sequenence Points
x = 4; // main intent is evaluationg expressions
y = 1 + x++;
while (x++ < 10) // (x++ < 10) is a full expression
printf("%d\n", x);
y = (4 + x++) + (6 + x++); // Not (4 + x++) nor (6 + x++) is a full expression
5.10 ์์๋ Flowcharts
int i = 0;
while (i < 10)
// an example of compoundstatements
// (block)
{
i++;
printf("%d\n", i);
}
5.11 ์๋ฃํ ๋ณํ Type Conversions
/* promotions in assignments */
short s = 64;
int i = s;
float f = 3.14f;
double d = f;
/* demotions in assignments */
d = 1.25;
f = 1.25;
//f = 1.123;
/* ranking of types in operations */
// long double > double > float
// unsigned long long, long long
// unsigned long, long
// unsigned, int
// short int, susigned short int
// signed char, char, unsigebd char
// _Bool
d = f + 1.234; // double + float ๋ float๋ฅผ double๋ก ๊ณ์ฐํจ
f = f + 1.234; // double์ float์ ๋์
ํ๋ ค ํ๋๊น warning
/* automatic promotion of function arguments */
// 1. Functions without prototypes
// 2. Varadic functions
/* casting operators */
d = (double)3.14f
i = 1.6 + 1.7; // = 3
i = (int)1.6 + (int)1.7; // = 2
/* more examples */
char c;
int i;
float f;
f = i = c = 'A'; // 65 // int๊ฐ float๋ก ๋ณํ๋จ
printf("%c%d %f\n", c, i, f) // A, 65, 65.00000 // float๋ฅผ int์ ๋์
ํ๊ธฐ ๋๋ฌธ์ ์ ์ญ๋จ
c = c + 2; // 'C', 67
i = f + 2 * c; // 65.0f + 2 * 67
printf("%c %d %f\n", c, i, f); // C, 199, 65.0000
c = 1106; // demolition, 1106 = 0b10001010010, 0b010010 = 1206 % 256 = 82 = 'R'
printf("%c\n", c);
c = 83.99;
printf("%c\n", c); // 'S', 83
5.12 ํจ์์ ์ธ์์ ๋งค๊ฐ๋ณ์ Arguments vs. Parameters
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void draw(int n); // ANSI function prototype declaration
int main()
{
int i = 5;
char c = '#'; // 35
float f = 7.1f;
draw(i);
draw((int)c);
draw((int)f);
/* Arguments vs. Parameters */
// actual argument, actual parameter -> argument (values)
// formal argument, formal parameter -> parameter (variables)
return 0;
}
void draw(int n)
{
while (n-- > 0)
printf("*");
printf("\n");
}
๋งค๊ฐ๋ณ์ : ํจ์์ ์ ์์์ ์ฌ์ฉ๋๋ ๋ณ์
์ธ์ : ํจ์๋ฅผ ํธ์ถํ ๋ ์ฌ์ฉ๋๋ ๊ฐ
'๋ฐ๋ฐฐ์จ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐ๋ฐฐ์จ 6.10 ~ (0) | 2021.01.12 |
---|---|
๋๋ฐฐ์จ 6.1 ~ (0) | 2021.01.11 |
๋ฐ๋ฐฐ์จ 4.5 ~ (0) | 2021.01.08 |
๋ฐ๋ฐฐ์จ 4.1 ~ (1) | 2021.01.07 |
๋ฐ๋ฐฐ์จ 3.7 ~ (0) | 2021.01.07 |