๐Ÿ’ก

๋”ฐ๋ฐฐ์”จ 5.1 ~ ๋ณธ๋ฌธ

๋”ฐ๋ฐฐ์”จ

๋”ฐ๋ฐฐ์”จ 5.1 ~

์•„์˜ณ์ด 2021. 1. 10. 18:08

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