Keyword:
์ฐ์ฐ์,
๋ณตํฉ๋์ ์ฐ์ฐ์,
์ฆ๊ฐ์ฐ์ฐ์,
์ฌ๋ณผ๋ฆญ ์์, ์ฌ์ฉ์ ์ ์ ์์
๋น๊ต์ฐ์ฐ์,
๋ ผ๋ฆฌ ์ฐ์ฐ์,
ํ๋ณํ,
๋ช ์์ ํ๋ณํ(๊ฐ์ ํ๋ณํ),
์๋ํ๋ณํ,
์ผํญ์ฐ์ฐ์(์กฐ๊ฑด์ฐ์ฐ์)
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
์ฃผ์์ฐ์ฐ์ ์ฌ์ฉ
#include<stdio.h>
void main() {
int i;
printf("์ ์์
๋ ฅ: ");
scanf("%d", &i);
printf("i = %d\n", i);
double d;
printf("์ค์์
๋ ฅ: ");
scanf("%lf", &d);
printf("d = %.2lf\n", d);
char c;
printf("๋ฌธ์์
๋ ฅ: ");
scanf(" %c", &c);
printf("c = %c\n", c);
}
์์ฒ๋ผ ์ ๋ ฅํ๋ฉด
์ด๋ ๊ฒ ์ถ๋ ฅ๋๋ค.
**์ฃผ์**
scanf() ์ฌ์ฉ ์ ๋ณดํต์ " " ์์ ๋น์นธ์ด ์์ผ๋ฉด ์๋์ง๋ง,
" " ์์ %c๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, %c ์์ ํ์นธ์ ๋์ด์จ์ผ ํ๋ค.
(๋ถํ์ํ enter๊ฐ ์ ๊ฑฐ)
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
๋ค์์ฒ๋ผ ์ ๋ ฅํ์ฌ ๋ฌธ์๋ ๊ธฐํธ๋ฅผ ์ซ์๋ก ํํํ ๊ฐ์ ๋ฌด์์ธ์ง ์์๋ผ ์ ๋ ์๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
#define SU 30 : SU๋ฅผ ์์ 30์ผ๋ก ์ ํ๋ค.
const: ๋ฐ๊ฟ ์ ์๋ ์์ ์ ์
- ๊ฐ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ
- ์์ํ
- ์ฌ๋ณผ๋ฆญ ์์ ๋ผ๊ณ ๋ถ๋ฅธ๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
๋์ ์ฐ์ฐ์, ์ฐ์ ์ฐ์ฐ์
ํ์ค์์์ ์ฌ์น์ฐ์ฐ๊ณผ๋ ๋ฌ๋ฆฌ
์ปดํจํฐ์์ ์ค์น์ฐ์ฐ์ด ์ฐ์ธ๋ค๊ณ ํ ์ ์๋ค.
๋ณตํฉ๋์ ์ฐ์ฐ์:
a = a+b ์ a+=b ๊ฐ ์๋ก ๊ฐ์๋ฐ,
์ด๋ ๊ฒ ์กฐ๊ธ๋ ๊ฐ๋จํ๊ฒ ๋ํ๋ธ ์ฐ์ฐ์๋ฅผ ๋ณตํฉ๋์ ์ฐ์ฐ์๋ผ๊ณ ํ๋ค.
๋ณตํฉ๋์ ์ฐ์ฐ์
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
์ฆ๊ฐ์ฐ์ฐ์ :
1์ฉ ์ปค์ง๊ฑฐ๋ 1์ฉ ์์์ง๋ ๊ฒฝ์ฐ๋ฅผ ๊ฐ๋จํ ์ฐ์ฐ์๋ก ๋ํ๋ธ ๊ฒ
i = i + 1
i += 1
i++
++i
์ ๋ค๊ฐ์ ์ ๋ชจ๋ ๊ฐ์ ์๋ฏธ์ด๋ค.
๋ค๋ง, ์ฐจ์ด๊ฐ ์๋ค๋ฉด ์ฐ์ ์์์์์ ์ฐจ์ด๊ฐ ์๋ค.
(์ปดํจํฐ๋ ํ๋ฒ์ ํ๊ฐ์ง ์ผ๋ง์ ์ฒ๋ฆฌํ ์ ์๋๋ฐ,
์ด๋ ๋จผ์ ์ฒ๋ฆฌํ๋ ๊ธฐ์ค์ ์ฐ์ ์์๋ผ๊ณ ํ๋ค.)
i++ : ํ์์ฆ๊ฐ ์ฐ์ฐ์. ์ฐ์ ์์๊ฐ ๋งค์ฐ ๋ฎ์
++i : ์ ์์ฆ๊ฐ ์ฐ์ฐ์. ์ฐ์ ์์๊ฐ ๋งค์ฐ ๋์
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
๊ทธ๋ ๋ค๋ฉด ์์ฒ๋ผ ์ ๋ ฅํ๋ค๋ฉด
์ถ๋ ฅ ์ ์ด๋ป๊ฒ ์ถ๋ ฅ๋ ๊น?
์ด๋ฐ ๊ฒฝ์ฐ ์ฝ๊ฒ ๊ฒฐ๊ณผ๊ฐ์ ์๊ธฐ์ํ ๋ฐฉ๋ฒ์ด ์๋ค.
๋๋ฒ๊น ํ๋ฅผ ์์ฑํ๋ ๊ฒ์ด๋ค.
n๋ฒ์งธ ์ค | a๊ฐ | b๊ฐ |
6๋ฒ์งธ ์ค | 10 | |
7๋ฒ์งธ ์ค | 11 | 10 |
9๋ฒ์งธ ์ค | 12 | 12 |
7๋ฒ์งธ ์ค: ์ฐ์ฐ์๊ฐ = ๊ณผ a++ ์ด ์๋๋ฐ
++๊ฐ ์ฐ์ ์์๊ฐ ๋งค์ฐ ๋ฎ์์
a๊ฐ์ 1์ด ๋ํด์ง๋ ๊ฒ๋ณด๋ค
b๊ฐ์ a๊ฐ์ด ๋์ ๋๋ ์ผ์ด ๋จผ์ ์ด๋ฃจ์ด์ก๋ค.
9๋ฒ์งธ ์ค: ์ฐ์ฐ์๊ฐ = ๊ณผ ++a ๊ฐ ์๋๋ฐ
++a๊ฐ ์ฐ์ ์์๊ฐ ๋งค์ฐ ๋์์
b๊ฐ์ a๊ฐ์ด ๋์ ๋๋ ์ผ๋ณด๋ค
a๊ฐ์ 1์ด ๋ํด์ง๋์ผ์ด ๋จผ์ ์ด๋ฃจ์ด์ก๋ค.
-->
๋ฐ๋ผ์ ์์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
๋น๊ต ์ฐ์ฐ์
T -> 1
F -> 0
ใ
กใ
กใ
กใ
กใ
ก
printf("10>5 = %d\n", 10 > 5); //10์ด 5๋ณด๋ค ํฐ๊ฐ? ->T ->1
printf("10>=5 = %d\n", 10 >= 5); //10์ด 5๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์๊ฐ? -> T->1
printf("10<5 = %d\n", 10 < 5); // 10์ด 5๋ณด๋ค ์์๊ฐ? ->F->0
printf("10<=5 = %d\n", 10 <= 5); //10์ด 5๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์๊ฐ? ->F->0
printf("10==5 = %d\n", 10 == 5); //10์ด 5์ ๊ฐ์๊ฐ? ->F ->0
printf("10!=5 = %d\n", 10 != 5); //10์ด 5์ ๋ค๋ฅธ๊ฐ? ->T ->1
์์ฒ๋ผ ์ถ๋ ฅ๋๋ค.
ใ กใ กใ กใ กใ ก
๋ ผ๋ฆฌ์ฐ์ฐ์
and :๊ทธ๋ฆฌ๊ณ ~์ด๊ณ , ~์ด๋ฉด์, ๋์์
or :๋๋ ํน์ ~์ด๊ฑฐ๋
not :๋ถ์
ใ
กใ
กใ
กใ
ก
printf("10<5 and 10>6 = %d\n", 10 < 5 && 10 > 6); //๋๋ค ๋ง์ผ๋ฉด T
printf("10<5 or 10>6 = %d\n", 10 < 5 || 10 > 6); // ๋์คํ๋๋ผ๋ ๋ง์ผ๋ฉด T
printf("!(10<5) = %d\n", !(10 < 5)); //๊ฒฐ๊ณผ๊ฐ์ ๋ฐ๋ ๋
ผ๋ฆฌ๊ฐ
๋ฐ๋ผ์ ์์ฒ๋ผ ์ถ๋ ฅ๋๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
ํ๋ณํ (data type)
ใ กใ กใ กใ กใ ก
int i = (int)3.94; // 3.94์ ์์์ ์ ๋ฒ๋ฆฌ๊ณ 3์ i์ ์ ์ฅํจ.
// ๋ช
์์ ํ๋ณํ==๊ฐ์ ํ๋ณํ
printf("i = %d\n", i); // i๊ฐ ์ถ๋ ฅ
double d = 3; //์ ์ 3์ ์์์ ์ ์ด๋ ค 3.000000์ d์ ์ ์ฅ.
// ์๋ ํ๋ณํ
printf("d = %lf\n", d); // d๊ฐ ์ถ๋ ฅ
int a = 10; // a์ ์ ์ 10 ์ ์ฅ.
int b = 3; // b์ ์ ์ 3 ์ ์ฅ.
printf("a/b = %lf\n", (double)a / b);
// a๊ฐ์ธ 10์ ์์์ ์ ์ด๋ ค 10.000000์ผ๋ก ๋ฐ๊พผ ํ b๊ฐ์ธ 3์ผ๋ก ๋๋์ด ๋ชซ์ธ 3.333333 ์ถ๋ ฅ.
printf("a/b = %lf\n", (double)(a / b));
// a/b๊ฐ์ธ 3์ ์์์ ์ ์ด๋ ค 3.000000์ผ๋ก ์ถ๋ ฅ.
ใ กใ กใ กใ กใ ก
์ด์ฒ๋ผ ์ถ๋ ฅ๋๋ค.
**์ฃผ์**
(double)a/b ์ (double)(a/b) ์ ์ฐจ์ด์ ์ด ๋ฌด์์ธ์ง ์์์ผํ๋ค.
์ฝ๊ฒ ๋งํด,
(double) ์ด a์๋ง ํด๋นํ๋์ง (a/b) ์ ์ฒด์ ํด๋นํ๋์ง์ ์ฐจ์ด์ด๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
์ด๋ฒ์ ๋ฌธ์ ๋ฅผ ํ๋ฒ ํ์ด๋ณด์.
ใ กใ กใ กใ กใ ก
ใ กใ กใ กใ กใ ก
์์ ๊ฐ์ด ์ ๋ ฅํ์ ๋, ์ด๋ป๊ฒ ์ถ๋ ฅ๋ ๊น?
|
|
|
|
|
|
์ ๋ต: 3 3 3 5
d>5 = 0
ํ์ด: ๋๋ฒ๊น ํ๋ถํฐ ๊ทธ๋ ค ๋ณด๊ฒ ๋ค.
a | b | c | d | |
4๋ฒ์งธ ์ค | 1 | |||
5๋ฒ์งธ ์ค | 2 | 2 | ||
6๋ฒ์งธ ์ค | 3 | 2 | ||
7๋ฒ์งธ ์ค | 3 | 3 | 5 |
-->
4๋ฒ์งธ ์ค: a์ 1๊ฐ์ ์ ์ฅํ๋ค.
5๋ฒ์งธ ์ค: ++a ์ = ์ค ++a๊ฐ ์ฐ์ ์์๊ฐ ๋๊ธฐ ๋๋ฌธ์
a๊ฐ์ 1์ ๋จผ์ ๋ํ๊ณ ,
b์ a๊ฐ์ ์ ์ฅํ๋ค.
6๋ฒ์งธ ์ค: = ์ a++์ค์ =์ด ์ฐ์ ์์๊ฐ ๋๊ธฐ ๋๋ฌธ์
a๊ฐ์ c์ ๋จผ์ ์ ์ฅํ๊ณ ,
a์ 1์ ๋ํ๊ณ ์ ์ฅํ๋ค.
7๋ฒ์งธ ์ค: = ๊ณผ b++ ๊ณผ ++c ๊ฐ ์๋๋ฐ,
์ฐ์ ์์์ ์ํด ++c , = , b++ ์์ผ๋ก ์ฐ์ฐํ๋ฏ๋ก
c๊ฐ์ 1์ ๋ํ๊ณ ,
b๊ฐ๊ณผ c๊ฐ์ ๋ํ์ฌ d์ ์ ์ฅํ๋ค.
๊ทธํ์, b๊ฐ์ 1์ ๋ํ๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
๊ทธ๋ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๊ฒ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
-->
int x, y; //x์ ๋ผ๋ ๊ณต๊ฐ ์์ฑ
printf("์ ์1์
๋ ฅ: ");
scanf("%d", &x); //์
๋ ฅํ๋ ์ฒซ๋ฒ์งธ ์๋ฅผ x์ ์ ์ฅ
printf("์ ์2์
๋ ฅ: ");
scanf("%d", &y); //์
๋ ฅํ๋ ๋๋ฒ์งธ ์๋ฅผ y์ ์ ์ฅ
int sum = x + y; //sum์ x+y๋ก ์ ์
double avg = sum *1.0 / 2;
// ํ๊ท ๊ฐ์ด ์ค์๋ก ํํ๋์๊ธฐ ๋๋ฌธ์ 1.0์ ๊ณฑํด์ ์ค์๋ก ํํํ๋ค. (ํน์ 2.0์ผ๋ก ๋๋์ด์ฃผ์ด๋ ๋๋ค.)
printf("๋์์ ์ดํฉ์ %d์ด๋ฉฐ, ํ๊ท ์ %.2lf์
๋๋ค.\n", sum, avg); // ์์์ ์๋ ๋์งธ์๋ฆฌ๊น์ง ํํํ๋๋ก ์ ํจ.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
์ผํญ์ฐ์ฐ์ (์กฐ๊ฑด์ฐ์ฐ์)
์กฐ๊ฑด์ ? true : false
์กฐ๊ฑด์์ด ์ฐธ์ด๋ฉด : ์์ ๊ฐ์,
์กฐ๊ฑด์์ด ๊ฑฐ์ง์ด๋ฉด : ๋ค์ ๊ฐ์ ์ถ๋ ฅํ๋ค.
์๋ฅผ ๋ค๋ฉด ์ด๋ฐ์์ผ๋ก ์ถ๋ ฅ๋๊ณค ํ๋ค.
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
์ด ์ฐ์ฐ์๋ฅผ ํตํด์ ๋ ์ซ์ ์ค ์ด๋ ์ซ์๊ฐ ํฐ ์ง ์์๋ด๋ ํจ์๋ฅผ ๋ง๋ค ์ ์๋ค.
int a, b; // a์ b ๊ณต๊ฐ ์์ฑ.
printf("์ ์ 2๊ฐ ์
๋ ฅ: ");
scanf("%d%d", &a, &b); ์
๋ ฅํ ๋ ์ซ์๋ฅผ ๊ฐ๊ฐ a์ b์ ์ ์ฅ.
int max = a > b ? a : b; // a>b๊ฐ ์ฐธ์ด๋ฉด a, a>b๊ฐ ๊ฑฐ์ง์ด๋ฉด b๋ฅผ ์ถ๋ ฅํ๋๋ก ์ค์ .
printf("๋์ค ๋ ํฐ๊ฐ์ %d์
๋๋ค.\n", max);
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
์ด๋ฅผ ํ์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ด ์ธ๊ฐ์ ์ ์ ์ค์ ๊ฐ์ฅ ์์ ์๋ฅผ ์ฐพ๋ ์ผ์ ํ ์ ๋ ์๋ค.
=======================================================
๋
๋๊ธ