
μ§μ λ³μ, μ μ λ³μ, μ μ λ³μ
CμΈμ΄μμ λ³μμ μ ν¨ λ²μμ μλͺ μ νλ‘κ·Έλ¨μ λμμ μ΄ν΄νκ³ μ μ΄νλ λ° μ€μν μν μ νλ€. μ΄ κΈμμλ CμΈμ΄μμ μ¬μ©λλ μΈ κ°μ§ μ£Όμ λ³μ μ νμΈ μ§μ λ³μ(Local Variables), μ μ λ³μ(Global Variables), μ μ λ³μ(Static Variables)μ λν΄ μ€λͺ νλ€.
β 1. μ§μ λ³μ (Local Variables)
μ§μ λ³μλ ν¨μ λ΄λΆμμ μ μΈλλ©°, ν΄λΉ ν¨μ λ΄μμλ§ μ κ·Όν μ μλ€. ν¨μμ νΈμΆμ΄ μμλ λ λ©λͺ¨λ¦¬μ ν λΉλκ³ , ν¨μμ μ€νμ΄ λλλ©΄ λ©λͺ¨λ¦¬μμ ν΄μ λλ€. μ΄λ¬ν νΉμ± λλ¬Έμ μ§μ λ³μλ ν¨μμ λ 립μ±κ³Ό μ¬μ¬μ©μ±μ λμ΄λ λ° κΈ°μ¬νλ€.
void function() {
int localVariable = 5; // μ§μ λ³μ μ μΈ
printf("%d\n", localVariable); // μ§μ λ³μ μ¬μ©
}
β 2. μ μ λ³μ (Global Variables)
μ μ λ³μλ ν¨μ μΈλΆμμ μ μΈλλ©°, νλ‘κ·Έλ¨μ μ΄λ κ³³μμλ μ κ·Όν μ μλ€. νλ‘κ·Έλ¨μ μμ μ λ©λͺ¨λ¦¬μ ν λΉλκ³ νλ‘κ·Έλ¨μ΄ μ’ λ£λ λκΉμ§ μ μ§λλ€. μ μ λ³μλ μ¬λ¬ ν¨μλ€ μ¬μ΄μμ λ°μ΄ν°λ₯Ό 곡μ ν νμκ° μμ λ μ μ©νμ§λ§, κ³Όλν μ¬μ©μ νλ‘κ·Έλ¨μ 볡μ‘μ±μ μ¦κ°μν€κ³ λλ²κΉ μ μ΄λ ΅κ² ν μ μλ€.
int globalVariable = 10; // μ μ λ³μ μ μΈ
void function() {
printf("%d\n", globalVariable); // μ μ λ³μ μ¬μ©
}
β 3. μ μ λ³μ (Static Variables)
μ μ λ³μλ static ν€μλλ₯Ό μ¬μ©νμ¬ μ μΈλλ€. μ μ λ³μλ μ§μ μ μ λ³μμ μ μ μ μ λ³μμ λ κ°μ§ ννλ‘ λλλ€.
1. μ§μ μ μ λ³μ: ν¨μ λ΄λΆμ μ μΈλλ©°, νλ‘κ·Έλ¨ μ€νμ΄ μμλ λ λ©λͺ¨λ¦¬μ ν λΉλκ³ νλ‘κ·Έλ¨μ΄ μ’
λ£λ λκΉμ§ μ μ§λλ€. κ·Έλ¬λ ν΄λΉ λ³μλ μ μΈλ ν¨μ λ΄μμλ§ μ κ·Όν μ μλ€. ν¨μ νΈμΆ κ°μ λ³μμ κ°μ΄ μ μ§λλ€. μ€μ νλ‘μ νΈμμλ μ¬μ©ν΄λ³Έ μ μλ μΌμ΄μ€μ΄λ€.
void function() {
static int staticLocalVariable = 0; // μ§μ μ μ λ³μ μ μΈ
staticLocalVariable++;
printf("%d\n", staticLocalVariable); // κ°μ ν¨μ νΈμΆ κ° μ μ§λ¨
}
2. μ μ μ μ λ³μ: ν¨μ μΈλΆμ μ μΈλμ§λ§ νμΌ λ΄λΆμμλ§ μ κ·Όν μ μλ€. νλ‘κ·Έλ¨μ λ€λ₯Έ νμΌλ€μμλ μ κ·Όν μ μμ΄, ν΄λΉ λ³μλ₯Ό μ¬μ©νλ μ½λμ λ²μλ₯Ό μ ννλ λ° λμμ΄ λλ€.
static int staticGlobalVariable = 20; // μ μ μ μ λ³μ μ μΈ
void function() {
printf("%d\n", staticGlobalVariable); // μ μ μ μ λ³μ μ¬μ©
}
β 4. Code
#include <stdio.h>
// μ μ λ³μ μ μΈ
int globalVariable = 10;
// μ μ μ μ λ³μ μ μΈ: μ΄ νμΌ λ΄μμλ§ μ κ·Ό κ°λ₯
static int staticGlobalVariable = 20;
// ν¨μ μ μΈ
void function(void);
void staticFunction(void);
int main() {
printf("μ μ λ³μ μ΄κΈ°κ°: %d\n", globalVariable);
printf("μ μ μ μ λ³μ μ΄κΈ°κ°: %d\n\n", staticGlobalVariable);
function();
function();
function();
staticFunction();
staticFunction();
return 0;
}
void function() {
// μ§μ λ³μ μ μΈ
int localVariable = 5;
printf("μ§μ λ³μ: %d\n", localVariable);
// μ§μ μ μ λ³μ μ μΈ: ν¨μ νΈμΆ κ° κ° μ μ§
static int staticLocalVariable = 0;
staticLocalVariable++;
printf("μ§μ μ μ λ³μ: %d\n", staticLocalVariable);
// μ μ λ³μμ μ μ μ μ λ³μ μ¬μ©
globalVariable += 10;
staticGlobalVariable += 10;
printf("μ μ λ³μ: %d\n", globalVariable);
printf("μ μ μ μ λ³μ: %d\n\n", staticGlobalVariable);
}
void staticFunction() {
// μ΄ ν¨μλ μ μ λ³μμ μ μ μ μ λ³μμ νμ¬ κ°μ 보μ¬μ€λ€.
printf("staticFunctionμμ λ³Έ μ μ λ³μ: %d\n", globalVariable);
printf("staticFunctionμμ λ³Έ μ μ μ μ λ³μ: %d\n\n", staticGlobalVariable);
}
μ§μ λ³μ, μ μ λ³μ, μ μ λ³μλ κ°κ° λ€λ₯Έ μ ν¨ λ²μμ μλͺ μ κ°μ§λ©°, CμΈμ΄ νλ‘κ·Έλλ°μμ μ€μν μν μ νλ€. μ§μ λ³μλ ν¨μμ λ 립μ±μ μ§μνκ³ , μ μ λ³μλ λ°μ΄ν° 곡μ λ₯Ό μ©μ΄νκ² νλ©°, μ μ λ³μλ λ©λͺ¨λ¦¬μ ν¨μ¨μ μ¬μ©κ³Ό λ³μμ μ κ·Ό λ²μλ₯Ό μ ννλ λ° μ¬μ©λλ€. μ΄λ¬ν λ³μλ€μ μ μ ν μ¬μ©νλ κ²μ νλ‘κ·Έλ¨μ ν¨μ¨μ±κ³Ό μ μ§λ³΄μμ±μ λμ΄λ λ° κΈ°μ¬νλ€.
'π· CμΈμ΄ 30κ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C/C++ Tip] 25. CμΈμ΄ 2μ°¨μ λ°°μ΄ (0) | 2024.11.10 |
---|---|
[C/C++ Tip] 24. CμΈμ΄ νμΌ μ μΆλ ₯ (1) | 2024.11.09 |
[C/C++ Tip] 22. CμΈμ΄ λ°λ³΅λ¬Έ : Whileλ¬Έ (0) | 2024.11.09 |
[C/C++ Tip] 21. CμΈμ΄ λ°λ³΅λ¬Έ: Forλ¬Έ (0) | 2024.11.09 |
[C/C++ Tip] 20. CμΈμ΄ 쑰건문 λ§μ€ν°νκΈ° : Switchλ¬Έ (1) | 2024.11.05 |

μ§μ λ³μ, μ μ λ³μ, μ μ λ³μ
CμΈμ΄μμ λ³μμ μ ν¨ λ²μμ μλͺ μ νλ‘κ·Έλ¨μ λμμ μ΄ν΄νκ³ μ μ΄νλ λ° μ€μν μν μ νλ€. μ΄ κΈμμλ CμΈμ΄μμ μ¬μ©λλ μΈ κ°μ§ μ£Όμ λ³μ μ νμΈ μ§μ λ³μ(Local Variables), μ μ λ³μ(Global Variables), μ μ λ³μ(Static Variables)μ λν΄ μ€λͺ νλ€.
β 1. μ§μ λ³μ (Local Variables)
μ§μ λ³μλ ν¨μ λ΄λΆμμ μ μΈλλ©°, ν΄λΉ ν¨μ λ΄μμλ§ μ κ·Όν μ μλ€. ν¨μμ νΈμΆμ΄ μμλ λ λ©λͺ¨λ¦¬μ ν λΉλκ³ , ν¨μμ μ€νμ΄ λλλ©΄ λ©λͺ¨λ¦¬μμ ν΄μ λλ€. μ΄λ¬ν νΉμ± λλ¬Έμ μ§μ λ³μλ ν¨μμ λ 립μ±κ³Ό μ¬μ¬μ©μ±μ λμ΄λ λ° κΈ°μ¬νλ€.
void function() {
int localVariable = 5; // μ§μ λ³μ μ μΈ
printf("%d\n", localVariable); // μ§μ λ³μ μ¬μ©
}
β 2. μ μ λ³μ (Global Variables)
μ μ λ³μλ ν¨μ μΈλΆμμ μ μΈλλ©°, νλ‘κ·Έλ¨μ μ΄λ κ³³μμλ μ κ·Όν μ μλ€. νλ‘κ·Έλ¨μ μμ μ λ©λͺ¨λ¦¬μ ν λΉλκ³ νλ‘κ·Έλ¨μ΄ μ’ λ£λ λκΉμ§ μ μ§λλ€. μ μ λ³μλ μ¬λ¬ ν¨μλ€ μ¬μ΄μμ λ°μ΄ν°λ₯Ό 곡μ ν νμκ° μμ λ μ μ©νμ§λ§, κ³Όλν μ¬μ©μ νλ‘κ·Έλ¨μ 볡μ‘μ±μ μ¦κ°μν€κ³ λλ²κΉ μ μ΄λ ΅κ² ν μ μλ€.
int globalVariable = 10; // μ μ λ³μ μ μΈ
void function() {
printf("%d\n", globalVariable); // μ μ λ³μ μ¬μ©
}
β 3. μ μ λ³μ (Static Variables)
μ μ λ³μλ static ν€μλλ₯Ό μ¬μ©νμ¬ μ μΈλλ€. μ μ λ³μλ μ§μ μ μ λ³μμ μ μ μ μ λ³μμ λ κ°μ§ ννλ‘ λλλ€.
1. μ§μ μ μ λ³μ: ν¨μ λ΄λΆμ μ μΈλλ©°, νλ‘κ·Έλ¨ μ€νμ΄ μμλ λ λ©λͺ¨λ¦¬μ ν λΉλκ³ νλ‘κ·Έλ¨μ΄ μ’
λ£λ λκΉμ§ μ μ§λλ€. κ·Έλ¬λ ν΄λΉ λ³μλ μ μΈλ ν¨μ λ΄μμλ§ μ κ·Όν μ μλ€. ν¨μ νΈμΆ κ°μ λ³μμ κ°μ΄ μ μ§λλ€. μ€μ νλ‘μ νΈμμλ μ¬μ©ν΄λ³Έ μ μλ μΌμ΄μ€μ΄λ€.
void function() {
static int staticLocalVariable = 0; // μ§μ μ μ λ³μ μ μΈ
staticLocalVariable++;
printf("%d\n", staticLocalVariable); // κ°μ ν¨μ νΈμΆ κ° μ μ§λ¨
}
2. μ μ μ μ λ³μ: ν¨μ μΈλΆμ μ μΈλμ§λ§ νμΌ λ΄λΆμμλ§ μ κ·Όν μ μλ€. νλ‘κ·Έλ¨μ λ€λ₯Έ νμΌλ€μμλ μ κ·Όν μ μμ΄, ν΄λΉ λ³μλ₯Ό μ¬μ©νλ μ½λμ λ²μλ₯Ό μ ννλ λ° λμμ΄ λλ€.
static int staticGlobalVariable = 20; // μ μ μ μ λ³μ μ μΈ
void function() {
printf("%d\n", staticGlobalVariable); // μ μ μ μ λ³μ μ¬μ©
}
β 4. Code
#include <stdio.h>
// μ μ λ³μ μ μΈ
int globalVariable = 10;
// μ μ μ μ λ³μ μ μΈ: μ΄ νμΌ λ΄μμλ§ μ κ·Ό κ°λ₯
static int staticGlobalVariable = 20;
// ν¨μ μ μΈ
void function(void);
void staticFunction(void);
int main() {
printf("μ μ λ³μ μ΄κΈ°κ°: %d\n", globalVariable);
printf("μ μ μ μ λ³μ μ΄κΈ°κ°: %d\n\n", staticGlobalVariable);
function();
function();
function();
staticFunction();
staticFunction();
return 0;
}
void function() {
// μ§μ λ³μ μ μΈ
int localVariable = 5;
printf("μ§μ λ³μ: %d\n", localVariable);
// μ§μ μ μ λ³μ μ μΈ: ν¨μ νΈμΆ κ° κ° μ μ§
static int staticLocalVariable = 0;
staticLocalVariable++;
printf("μ§μ μ μ λ³μ: %d\n", staticLocalVariable);
// μ μ λ³μμ μ μ μ μ λ³μ μ¬μ©
globalVariable += 10;
staticGlobalVariable += 10;
printf("μ μ λ³μ: %d\n", globalVariable);
printf("μ μ μ μ λ³μ: %d\n\n", staticGlobalVariable);
}
void staticFunction() {
// μ΄ ν¨μλ μ μ λ³μμ μ μ μ μ λ³μμ νμ¬ κ°μ 보μ¬μ€λ€.
printf("staticFunctionμμ λ³Έ μ μ λ³μ: %d\n", globalVariable);
printf("staticFunctionμμ λ³Έ μ μ μ μ λ³μ: %d\n\n", staticGlobalVariable);
}
μ§μ λ³μ, μ μ λ³μ, μ μ λ³μλ κ°κ° λ€λ₯Έ μ ν¨ λ²μμ μλͺ μ κ°μ§λ©°, CμΈμ΄ νλ‘κ·Έλλ°μμ μ€μν μν μ νλ€. μ§μ λ³μλ ν¨μμ λ 립μ±μ μ§μνκ³ , μ μ λ³μλ λ°μ΄ν° 곡μ λ₯Ό μ©μ΄νκ² νλ©°, μ μ λ³μλ λ©λͺ¨λ¦¬μ ν¨μ¨μ μ¬μ©κ³Ό λ³μμ μ κ·Ό λ²μλ₯Ό μ ννλ λ° μ¬μ©λλ€. μ΄λ¬ν λ³μλ€μ μ μ ν μ¬μ©νλ κ²μ νλ‘κ·Έλ¨μ ν¨μ¨μ±κ³Ό μ μ§λ³΄μμ±μ λμ΄λ λ° κΈ°μ¬νλ€.
'π· CμΈμ΄ 30κ°' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[C/C++ Tip] 25. CμΈμ΄ 2μ°¨μ λ°°μ΄ (0) | 2024.11.10 |
---|---|
[C/C++ Tip] 24. CμΈμ΄ νμΌ μ μΆλ ₯ (1) | 2024.11.09 |
[C/C++ Tip] 22. CμΈμ΄ λ°λ³΅λ¬Έ : Whileλ¬Έ (0) | 2024.11.09 |
[C/C++ Tip] 21. CμΈμ΄ λ°λ³΅λ¬Έ: Forλ¬Έ (0) | 2024.11.09 |
[C/C++ Tip] 20. CμΈμ΄ 쑰건문 λ§μ€ν°νκΈ° : Switchλ¬Έ (1) | 2024.11.05 |