๋ฐ๋ฐฐ์จ 11.10 ~
11.10 ๋ช ๋ น์ค ์ธ์ Command-Line Arguments
-๋ช ๋ น์ค ์ธ์๋ ํ๋ก๊ทธ๋จ ์คํ ์์ argument๋ก ๋ฃ์ด์ค ์ ์๋ค.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(int argc, char * argv[])
{
int count;
printf("The command line has %d arguments:\n", argc);
for (count = 0; count < argc; count++)
printf("Arg %d : %s\n", count, argv[count]);
printf("\n");
return 0;
}
๋ช ๋ น์ค ์ธ์๋ก์ ๋ฌธ์์ด์ ๋ฐฐ์ด์ argv[]์ ๋ฐ์๋ค์ด๊ณ ๋ฐฐ์ด์ ๊ฐ์๋ฅผ argc๋ก ๋ฐํํ๋ ์ฝ๋
11.11 ๋ฌธ์์ด์ ์ซ์๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ๋ค
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
/*
string to integer, double, long
atoi(), atof(), atol()
*/
if (argc < 3)
printf("Wrong usage of %s\n", argv[0]); // ๋ฐฐ์ด์ 1๋ฒ์งธ ๋ฉ๋ชจ๋ฆฌ๋ ์คํํ์ผ๋ช
else
{
// Example 1
int times = atoi(argv[1]); // ๋ฐฐ์ด์ 2๋ฒ์งธ ๋ฌธ์์ด์ ์ ์๋ก ๋ฐ๊ฟ times์ ๋์
// atof(), atol()
for (int i = 0; i < times; i++)
puts(argv[2]); // ๋ฐฐ์ด์ 3๋ฒ์งธ ๋ฌธ์์ด์ times์ ๋ค์ด์จ ์ ์๋งํผ ์ถ๋ ฅ
//Example 2
printf("Sum = %d\n", atoi(argv[1] + atoi(argv[2])));
}
/*
string to long, unsigned long, double
strtol(), strtoul(), strtod()
strtod() converts base 10 only
*/
char str1[] = "1024Hello";
char* end;
long l = strtol(str1, &end, 10); // &end ์ด์คํฌ์ธํฐ, 10 ์ญ์ง์
printf("%s %ld %s %d\n", str1, l, end, (int)*end);
// %s str1 : ๋ณํํ๋ ค๋ ๋ฌธ์์ด
// %ld l : ์ซ์ ๋ถ๋ถ๋ง
// %s end : strtol ์์ ๋ฌธ์์ด์ ์ซ์๋ก ๋ฐ๊พธ๋ค๊ฐ ๋ณํํ ์ ์๋ ์์น์ ์ค๋ฉด ๊ทธ ํฌ์ธํฐ๋ฅผ end์ ๋ฃ์ด์ค
// %d (int)* end : 'H'์ ํด๋นํ๋ ์์คํค์ซ์
char str2[] = "10FFHello";
l = strtol(str2, &end, 16);
printf("%s %ld %s %d\n", str2, l, end, (int)* end);
/*
Numvers to strings
Use strintf() instead of itoa(), ftoa()
*/
char temp[100];
puts(_itoa(10, temp, 16)); // ๊ถ์ฅํ์ง ์์
sprintf(temp, "%x", 10); //a //printfํ ๋ด์ฉ์ ์ ๋ถ ๋ฌธ์์ด์ ์ ์ฅ ์ซ์ 10์ 16์ง์(%x)๋ฌธ์๋ก
puts(temp);
return 0;
}
12.1 ๋ฉ๋ชจ๋ฆฌ ๋ ์ด์์ ํ์ด๋ณด๊ธฐ
-Text Segment : ์ฝ๋ ์ธ๊ทธ๋จผํธ(๊ธฐ๊ณ์ด๋ก ๋ณํ๋ ์ฝ๋)
-Data Segment : ์ด๊ธฐํ๋ ์ ์ญ,์ ์ ๋ณ์ ์ฐ๊ธฐ ๊ฐ๋ฅ (์ฌ๊ธฐ ์ ์ฅ๋ ๋ฌธ์์ด์ Read Only Data Segment์ ์ ์ฅ๋จ)
-BBS Segment : ์ด๊ธฐํ ๋์ง ์์ ์ ์ญ, ์ ์ ๋ณ์
-Heap : ๋์ ํ ๋น
-Stack : ์ง์ญ๋ณ์, ๋งค๊ฐ๋ณ์
- Text Segment: ํ๋ก๊ทธ๋จ ์คํ ์ ํ๋ก๊ทธ๋จ ์์ฒด๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ๊ณ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ๊ฒ ๋๋ค
-char* str = "Hello, world"; ์์ ๋ฌธ์์ด Hello, World๋ Date Segment์ค Read Only Data Segment์ ์ ์ฅ๋จ
๋ฐ๋ผ์ ํฌ์ธํฐ๋ฅผ ํตํด str[0] = 'M';์ ์๋ฌ๋ฅผ ์ผ์ผํจ๋ค. ํ์ง๋ง arr์ ๊ฒฝ์ฐ stack ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ๋ค
- ์ด๋ค ๋ณ์๋ค์ ํ๋ก๊ทธ๋จ ์ ์ฒด์์ ๊ณ์ ์ฌ์ฉ๋๋ค. ์ด๋ฌํ ์ ์ญ ๋ณ์๋ BBS Segment, Data Segment์ ์ ์ฅ๋๋ค
-์ง์ญ์ ์ผ๋ก๋ง ์ฌ์ฉํ ์ ์๋ ์ง์ญ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ด ์ข์์ง๋ค ์ด๋ฌํ ์ง์ญ๋ณ์๋ ์คํ ๋ฉ๋ชจ๋ฆฌ์ ์์ธ๋ค
๋ธ๋ก ์์ด ์คํ๋ ๋๋ง stack ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ง์ญ๋ณ์๋ฅผ ์ฐ๊ณ , ๊ทธ ์ธ์ ์ฐ์ง ์๋๋ค
์คํ ๋ฉ๋ชจ๋ฆฌ์ ํน์ง ์ค ํ๋๊ฐ ์ง์ญ๋ณ์๋ ์ปดํ์ผ๋ฌ๊ฐ ์คํ ์์ ์ด๋ฏธ ๋ชจ๋ ์ ํด์ง๋ค. ๋ฐ๋ผ์ ์คํ ๋ฉ๋ชจ๋ฆฌ๋ ์ ๊ทผ์ด ๋น ๋ฅด๋ค. ํ์ง๋ง ํ์ํ ๋ฉ๋ชจ๋ฆฌ์ ํฌ๊ธฐ๋ฅผ ๋ฏธ๋ฆฌ ์ ์ ์์ ๊ฒฝ์ฐ์ ์คํ ๋ฉ๋ชจ๋ฆฌ์ ๋ด์ ์ ์๋ค.
-์ด๋ ์ฌ์ฉํ๋ ๊ฒ์ด ๋์ ํ ๋น ์ด๋ผ๊ณ ํ๋ค(Heap ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ) Heap ๋ฉ๋ชจ๋ฆฌ๋ ์ปดํ์ผํ ๋๋ ์ฌ์ด์ฆ๋ฅผ ์ ์๊ฐ ์๋ค.
๋จ์ ์ ์ฃผ์๋ฅผ ๋ฐฐ์ ๋ฐ์ ๋ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ผ๋ง๋ ํ์ํ์ง ์ด์์ฒด์ ์ ์์ฒญํ๊ณ ๋ฐ์์ค๋ ๊ตฌ์กฐ์ด๊ธฐ ๋๋ฌธ์ ์กฐ๊ธ ๋๋ฆฌ๋ค
๋ํ Heap์ ๋ฉ๋ชจ๋ฆฌ๋ ๋ค ์ฐ๊ผฌ ๊ผญ ๋ฐ๋ฉ์ ํด์ผ ํ๊ธฐ ๋๋ฌธ์ free(arr) ๋ฑ์ ์จ์, ํ๋ก๊ทธ๋๋จธ๊ฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ฐ๋ฉํ๋ค.
12.2 ๊ฐ์ฒด(object)์ ์๋ณ์(identifier), L-value์ R-value
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
/*
Object
- "An object is simply a block of memory that can store a value."
- Object has more developed meaning in C++ and Object Oriented Programing (OOP)
Identifiers
- Names for variables, functions, macros, and other entities
*/
int var_name = 3; // ceates an object called 'var_name'
int* pt = &var_name; // pt is an identifier ํฌ์ธํฐ๋ณ์ ์์ฒด๋ ์ฃผ์๋ฅผ ๊ฐ์ง
*pt = 1; // *pt is not an identifier. *pt designates an object
int arr[100]; // arr is an identifier. Is arr an object?
// ๋ฐฐ์ด๋ช
์ ์๋ณ์๋ ๋ง์ง๋ง, object๋ ์๋๋ค - ์ฒซ์์์ ๋ํ ์ฃผ์์ด์ง ๊ทธ ์์ฒด๊ฐ ๋ฉ๋ชจ๋ฅด๋ฆด ๊ฐ์ง ์๋๋ค
arr[0] = 7; // arr[0] is an object
/*
Lvalue is an expression 'referring' to an object.
L-value : left side of an assignment
R-vlaue : right side, variable, constant, expressions
*/
var_name = 3; // modifiable lvalue
int temp = var_name; // Lvalue๊ฐ Rvalue๋ก ์ฌ์ฉํ ์ ์๊ณ ์ด๋๋ ๋จ์ํ ๊ฐ์ ์ ๋ฌํ๋ ์ญํ
pt = &var_name;
int* ptr = arr;
*pt = 7; // *pt is not an identifier but an modifiable lvalue expression
int* ptr2 = arr + 2 * var_name; // address rvalue
*(arr + 2 * var_name) = 456; // lvalue expression
const char* str = "Constant string"; // str is a modifiable lvalue
str = "Second string"; // "Constant string" = "Second string" // impossible
//str[0] = 'A'; // Error
//puts(str);
char str2[] = "String in an array";
str2[0] = 'A'; // OK
//puts(str2);
return 0;
}
12.3 ๋ณ์์ ์์ญ(scope)๊ณผ ์ฐ๊ฒฐ(Linkage) ์ํ, ๊ฐ์ฒด์ ์ง์๊ธฐ๊ฐ(duration)
Scope
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*
Variable scopes (visibility)
- block, function, fuction prototype, file
*/
int g_i = 123; // global variable
int g_j;
void func1()
{
g_i++; // uses g_i
}
void func2()
{
g_i += 2;
}
int main()
{
int local = 1234;
func1();
func2();
printf("%d\n", g_i);// uses g_i
printf("%d\n", g_j);// Not initialized? // 0 ์ด๊ธฐํ ๋์ง ์์ ์ ์ญ๋ณ์๋ ์ปดํ์ผ๋ฌ๊ฐ 0์ผ๋ก ์ด๊ธฐํ
printf("%d\n", local);
return 0;
}
1. block { } : { } ์์ ์ ์ธ๋ ๋ณ์๋ { } ์์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
2. function : ํจ์ ์์์ ์ ์ธ๋ ๋ณ์์ ์์ญ์ ํจ์์
3. function prototype : ํ๋กํ ํ์ ์ ๊ฒฝ์ฐ ํ๋กํ ํ์ ๋ด์์๋ง visibility. (๋ฐ๋ผ์ ์๋ฃ ํ๋ง ์ฐ๊ณ ๋ณ์ ์๋ต ๊ฐ๋ฅ)
4. file scope: ํ๋ก๊ทธ๋๋ฐ ๋ฐ๊นฅ ๋ถ๋ถ(ex. ์ ์ญ๋ณ์) - ์ฌ๊ธฐ์ ์๋ ๋ณ์๋ ํ์ผ ์์์ ์ด๋๋ ์ง ์ฌ์ฉ
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*
Variable scopes (visibility)
- block, function, fuction prototype, file
*/
void f1(int hello, double world); // to the end of the prototype declaration
//void vla_param(int n, int m, double ar[n][m]); // gcc only
double func_block(double d)
{
double p = 0.0;
int i;
for (i = 0; i < 10; i++)
//for (int i = 0; i < 10; i++)
{
double q = d * i;
p *= q;
if (i == 5)
goto hello;
}
hello:
printf("Hello, World");
return p;
}
int main()
{
func_block(1.0);
}
void f1(int hello, double world)
{
}
Linkage
ํ ํ๋ก์ ํธ ์์ ๋ ๊ฐ์ ํ์ผ์ Linkage๋ฅผ ํตํด ์๋ก ๋ณ์๋ฅผ ๊ณต์ ํ ์ ์๋ค.
์ค๋ฅธ์ชฝ 3๋ฒ์งธ ์ค์ int el;์ extern์ ๋ถ์ด๋ฉด '์ด๋๊ฐ ์ ์ธ๋์ด ์๋ int el์ ์จ๋ผ' ๋ ์๋ฏธ์ด๋ค.
๋ฐ๋ฉด ์ผ์ชฝ 15๋ฒ์งธ ์ค์ static int il;์ 'int il;์ ์กด์ฌํ๋ scope ๋ด์์๋ง ์กด์ฌํ๋ค'๋ ์๋ฏธ์ด๋ค
์ผ์ชฝ 17๋ฒ์งธ ์ค์์ void testLinkage(); ๋ฅผ ๋ณด๋ฉด ํ๋กํ ํ์ ์ ์จ์ ๋ค๋ฅธ ํ์ผ์ ํจ์๋ฅผ ์ธ ์ ์๋ค.
Duration
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*
Storage duration:
- static storageduration // ํ๋ก๊ทธ๋จ์ด ์์๋ ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ก๊ณ ๋๋ ๋๊น์ง ์ ์ง
(Note: 'static' keyword indicates the linkage type, not the storage duration)
- automatoc storage duration // stack์ ์ ์ฅ, ์ง์ญ๋ณ์
- allocated storage duration // ๋์ ํ ๋น
- thread storage duration // ๋ฉํฐ์ฐ๋ ๋ฉ
*/
void count()
{
int ct = 0; // ํ์ฌ block ์์์ ๋
printf("count = %d\n", ct);
ct++;
}
void static_count()
{
static int ct = 0; // ํ๋ก๊ทธ๋จ์ด ์์๋ ๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ก๊ณ ๋๋ ๋๊น์ง ์ ์ง
printf("static count = %d\n", ct);
ct++;
}
int main()
{
count(); // 0
count(); // 0
static_count(); // 0
static_count(); // 1
return 0;
}
12.4 ์ ์ฅ ๊ณต๊ฐ์ ๋ค์ฏ ๊ฐ์ง ๋ถ๋ฅ
2๊ฐ์ง๋ก ๋ถ๋ฅํ๊ธฐ๋ ํ๋ค (static๊ณผ static์ด ์๋ ๊ฒ)
์๋๋ณ์ : ์ง์ญ๋ณ์ - ์ง์ ๊ธฐ๊ฐ์ด ์๋์ ์ผ๋ก ๊ฒฐ์
๋ ์ง์คํฐ๋ณ์ : CPU ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฐ๋ ๊ฒ์ผ๋ก ์์ฒญ ๋น ๋ฆ - ์ค์ง์ ์ผ๋ก ๋ง์ด ์ ์ฐ์ธ๋ค
Static : ๋ฉ๋ชจ๋ฆฌ์ ๊ณ ์ ๋์ด ์๋ค (์ปดํ์ผ๋ฌ์ ๋ฒ์ญ๋จ์: ํ์ผ)
ํ ๋น๋ฉ๋ชจ๋ฆฌ : ๋ฐํ์์์ ์ด์์ฒด์ ์๊ฒ ์์ฒญํด์ 'ํฌ์ธํฐ'๋ง ๋ฐ๋๋ค. ์ฆ, ์๋ณ์๊ฐ ์๋ค
12.5 ์๋๋ณ์ Automatic Variables
19๋ฒ์งธ ์ค์์ ์คํ์ int b = 123 ์ด ์์๋ค๊ฐ
21๋ฒ์งธ ์ค์์ scope๋ฅผ ๋น ์ ธ๋์ค๋ฉฐ ์คํ์์ ๋น ์ง๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
/*
Automatic storage class
- Automatic storage duration, block scope, no linkage
- Any variable declared in a block or function header
*/
void func(int k);
int main() //Note: main() is a function.
{
auto int a; // keyword auto : a storage-class specifier
a = 1024;
//printf("%d\n", a); // whaat happens if uninitialized?
//auto int b = a * 3; // whaat happens if uninitialized?
// bss ์ ์๋ ๋ณ์๋ค์ ์ปดํ์ผ๋ฌ๊ฐ ์์์ 0์ผ๋ก ์ด๊ธฐํ ์์ผ์ฃผ์ง๋ง,
// ์๋๋ณ์๋ ์คํ์ ์์ฑ๋ ๋๋ง๋ค ์ด๊ธฐํํด์ฃผ๋ฉด "๋น์ฉ"์ด ๋ฐ์ํ๋ฏ๋ก ๋ณ๋๋ก ์ด๊ธฐํ ์์์ผ์ค
// ๋ฐ๋ผ์ "์๋๋ณ์๋ ๋ฐ๋์ ์ด๊ธฐํ ์์ผ์ค์ผํจ"(c++ ์์๋ ์ด๋ฐ ๋ถํธํจ์ ๋๋๋ผ๋๋ก ์ฌ๋ฌ๊ฐ์ง ์ฅ์น๊ฐ ๋์๋ค๊ณ ํจ
int i = 1;
int j = 2;
printf("i %lld\n", (long long)&i);
{
int i = 3; //name hiding
printf("i %lld", (long long)&i);
int ii = 123;
// j is visible here
printf("j = %d\n", j);
}
// ii is not visible here
printf("i %lld\n", (long long)&i); // which i?
for (int m = 1; m < 2; m++)
printf("m %lld\n", (long long)&m); // no block?
func(5); // cannot see any of the variabled defined so far
for (int m = 3; m < 4; m++)
{
printf("m%lld\n", (long long)&m); // block?
}
return 0;
}
void func(int k)// stack frame ์ด ๋ฐ๊ปด์ mainํจ์์์ ์ฐ๋ ๋ณ์๋ค์ ์ ๊ทผํ ์ ์๋ค
{
int i = k + 2;
// do something with i and k
printf("i %lld\n", (long long)&i);
}