๐ก
๋ฐ๋ฐฐ์จ 9.1 ~ ๋ณธ๋ฌธ
9.1 ํจ์๊ฐ ํ์ํ ๋
-์ผ๋ฐํ(๋ค์ํ ๊ฒฝ์ฐ์ ์ฑ๋ฆฝํ ์ ์๋๋ก)
-๊ฐ์ ๊ธฐ๋ฅ์ด ์ฌ๋ฌ๋ฒ ๋์ฌ ๋
-์ง์์ ์ผ๋ก ์ ์ง๋ณด์๊ฐ ํ์ํ๋ ค๋ฉด
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> // strlen()
#include <stdbool.h>
#define WIDTH 30
#define NAME "Na-Da Ga"
#define ADDRESS "Seoul, Korea"
void print_chars(char c, int n_stars, bool endl)
{
for (int i = 0; i < n_stars; i++)
printf("%c",c);
if (endl == true)
printf("\n");
}
void print_centered_str(char str[])
{
int n_blanks = 0;
n_blanks = (WIDTH - strlen(str)) / 2;
print_chars(' ', n_blanks, false);
printf("%s\n", str);
}
int main()
{
print_chars('*', WIDTH, true);
print_centered_str(NAME);
print_centered_str(ADDRESS);
print_chars('*', WIDTH, false);
return 0;
}
9.2 ํจ์์ ํ๋กํ ํ์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h> // strlen()
#include <stdbool.h>
#define WIDTH 30
#define NAME "Na-Da Ga"
#define ADDRESS "Seoul, Korea"
void print_centered_str(char str[]);
void print_chars(char c, int n_stars, bool newline);// prototype
int main()
{
print_chars('*', WIDTH, true);// argument
print_centered_str(NAME);
print_centered_str(ADDRESS);
print_chars('*', WIDTH, false);
return 0;
}
void print_centered_str(char str[])
{
int n_blanks = 0;
n_blanks = (WIDTH - strlen(str)) / 2;
print_chars(' ', n_blanks, false);
printf("%s\n", str);
}
void print_chars(char c, int n_stars, bool newline)// parameter
{
for (int i = 0; i < n_stars; i++)
printf("%c", c); // putchar(c)
if (newline == true)
printf("\n");
}
9.1์ ํจ์๋ค์ ํ๋กํ ํ์ ํ ํ ๋ชจ์ต
9.3 ํจ์์ ์๋ฃํ๊ณผ ๋ฐํ๊ฐ
์ ์ํ ์๋ฃํ int๋ ์๋ต์ด ๊ฐ๋ฅํ๋ค
์ปดํ์ผ๋ฌ๊ฐ ๋ฐํ ์๋ฃํ์ด ์๋ต๋๋ฉด ์์์ int๋ก ์๊ฐํด์ ์ปดํ์ผ ํ๋ค.But ํ์คํ ํํํด์ฃผ๋๊ฒ ์ข๋ค
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int int_min(int, int);
int main()
{
int i1, i2;
while (1)
{
printf("Input two integers : ");
if (scanf("%d %d", &i1, &i2) != 2) break;
int lesser = int_min(i1, i2);
//int lesser = min;
printf("The lesser of %d and %d is %d\n", i1, i2, lesser);
//printf("The lesser of %d and %d is %d\n", i1, i2, int_min(i1, i2));
}
printf("End.\n");
return 0;
}
int int_min(int i, int j)
//int_min(int i, int j)
{
int min;
if (i < j)
min = i;
else
min = j;
return min;
// return (i < j) ? i : j;
// return (float)min; warning
/*
if (i < j)
return i;
else
return j;
printf("Who am I?"); ์คํ ์๋จ
*/
//return; //return with no value
}
์ฝ๋ ์๋์ชฝ์
return (float)min;
์ min์ float์ผ๋ก ํ๋ณํ ํด์ค๋ ํจ์์ ๋ฐํ์๋ฃํ์ด int๋ก ์ง์ ๋์ด ์์ด
๋ค์ float -> int๋ก ํ๋ณํ์ด ์ผ์ด๋ warning์ด ๋ฌ๋ค
9.4 ๋ณ์์ ์์ญScope๊ณผ ์ง์ญ ๋ณ์Local Variable
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int int_max(int i, int j)
{
//a = 456;
int m;
m = i > j ? i : j;
return m;
}
int main()
{
//m = 123;
int a;
a = int_max(1, 2);
printf("%d\n", a);
printf("%p\n", &a); // ์ฃผ์๊ฐ์ ์ง์ ์ถ๋ ฅ
{
//int a;
a = int_max(4, 5);
printf("%d\n", a);
printf("%p\n", &a);
int b = 123;
}
//b = 456;
printf("%d\n", a);
printf("%p\n", &a);
return 0;
}
์์ ์ฝ๋๋ฅผ ์คํ์ํค๋ฉด
2
00CFFE2C
5
00CFFE2C
5
00CFFE2C
mainํจ์์ ์์ ๋ธ๋ญ์ int a ์ ์ฃผ์์ ํ๊ณ ์คํ์ํค๋ฉด
2
001DF9BC
5
001DF9B0
2
001DF9BC
์ด๋ ๊ฒ ์ฒซ๋ฒ์งธ๋ a์ ์ฃผ์๊ฐ ๋ชจ๋ ๊ฐ์ง๋ง ๋๋ฒ์งธ์์ ์์ ๋ธ๋ญ์ a๋ ์ฃผ์๊ฐ ๋ค๋ฅธ๊ฑธ ๋ณผ ์ ์๋ค.
-{ } ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ณ์์ ์์ญ์ ๋๋๊ณ ๋ฐ์ ๋ธ๋ญ์์ ์ ์ธํ ๋ณ์๋ ์์ ๋ธ๋ญ์์๋ ์ฌ์ฉ์ด ๊ฐ๋ฅ ํ์ง๋ง
์์ ๋ธ๋ญ์์ ์ ์ธํ ๋ณ์๋ฅผ ๋ฐ์ ๋ธ๋ญ์์ ์ฌ์ฉํ ์๋ ์๋ค.
9.5 ์ง์ญ ๋ณ์์ ์คํStack
-'์๋ค' '์์์ฌ๋ฆฐ ๋๋ฏธ'
-์ฒ์ ๋ค์ด์จ ๋ฐ์ดํฐ๊ฐ ๊ฐ์ฅ ์๋์ ๋์ด๊ณ ๋ง์ง๋ง์ ์์ธ๊ฒ์ด ๋จผ์ ๋๊ฐ๋ ๊ตฌ์กฐ.
-int i; ๋ผ๊ณ ๋ณ์๋ฅผ ์ ์ธํ๋ฉด i๋ ๊ทธ์ ์ด๋ฆ์ผ ๋ฟ, ์ค์ ๋ก ์ ๊ทผํ๋ ๊ฒ์ ์ฃผ์๋ค. ์ฃผ์๋ฅผ ํตํด ๊ฐ์ ์ ๊ทผํ๋ค
9.6 ์ฌ๊ท ํธ์ถ Recursion
-๋ฌดํ๋ฃจํ ์ฌ์ฉํ ๋ ์ฌ์ฉ X while, for ์ฌ์ฉ
-stop condition์ ์ฌ์ฉํ์
-์ธ์ ์ ์ฒ๋ผ ๊ฟ์์ ๊ฟ์ผ๋ก ๋ค์ด๊ฐ๊ณ ๊ฐ์ฅ ๊น์ ๊ฟ์์ ๊นจ๊ณ ๋ ๊ทธ ๊ฟ์์ ๊นจ๊ณ ๋ฅผ ๋ฐ๋ณตํ๋ ๋๋
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void my_func(int);
int main()
{
my_func(1);
return 0;
}
void my_func(int n)
{
printf("Level %d, address of variable n = %p\n", n, &n);
if (n < 4) // stop condition
my_func(n + 1);
printf("Level %d, address of variable n = %p\n", n, &n);
}
9.7 ์ฌ๊ท ํธ์ถ๊ณผ ์คํ
Level 1, address of variable n = 19920948
Level 2, address of variable n = 19920732
Level 3, address of variable n = 19920516
Level 4, address of variable n = 19920300
Level 4, address of variable n = 19920300
Level 3, address of variable n = 19920516
Level 2, address of variable n = 19920732
Level 1, address of variable n = 19920948
9.6์ ์ฝ๋์์ ์ฃผ์๋ฅผ ์ญ์ง์๋ก ๋ฐ๊ฟ์ ์คํํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๋์นญ์ผ๋ก ์ฃผ์๊ฐ ๊ฐ์๊ฒ์ ๋ฐ๊ฒฌํ ์ ์๋ค
9.8 ํฉํ ๋ฆฌ์ผ ์์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
long loop_factorial(int n);
long recursive_factorial(int n);
int main()
{
int num = 5;
printf("%d\n", loop_factorial(num));
printf("%d\n", recursive_factorial(num));
return 0;
}
long loop_factorial(int n)
{
long fac;
for (fac = 1; n > 1; n--)
{
fac *= n;
}
return fac;
}
long recursive_factorial(int n)
{
if (n > 0)
{
return n * recursive_factorial(n - 1); // tail recursion
}
else
return 1;
}
9.9 ์ด์ง์ ๋ณํ ์์
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*
10
10 / 2 = 5, remainder = 0
5 / 2 = 2, remainder = 1
2 / 2 = 1, remainder = 0
1 / 2 = 0, remainder = 1
*/
void print_binary(unsigned long n);
void print_binary_loop(unsigned long n);
int main()
{
unsigned long num = 10;
print_binary_loop(num); // 0101
print_binary(num); // 1010
printf("\n");
return 0;
}
void print_binary_loop(unsigned long n)
{
while (1)
{
int quotient = n / 2;
int remainder = n % 2;
printf("%d", remainder);
n = quotient;
if (n == 0) break;
}
printf("\n");
}
void print_binary(unsigned long n)
{
int remainder = n % 2;
if (n >= 2)
print_binary(n / 2);
printf("%d", remainder);
return;
}
while๋ฌธ์ ์ด์ฉํ๋ฉด ์ด์ง์๊ฐ ๊ฑฐ๊พธ๋ก ์ถ๋ ฅ๋๋ค.
'๋ฐ๋ฐฐ์จ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐ๋ฐฐ์จ 10.1 ~ (0) | 2021.01.25 |
---|---|
๋ฐ๋ฐฐ์จ 9.10 ~ (0) | 2021.01.21 |
๋ฐ๋ฐฐ์จ 7.10 ~ (0) | 2021.01.18 |
๋ฐ๋ฐฐ์จ 7.2 ~ (0) | 2021.01.15 |
๋ฐ๋ฐฐ์จ 6.10 ~ (0) | 2021.01.12 |