지역 λ³€μˆ˜, μ „μ—­ λ³€μˆ˜, 정적 λ³€μˆ˜

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μ–Έμ–΄ ν”„λ‘œκ·Έλž˜λ°μ—μ„œ μ€‘μš”ν•œ μ—­ν• μ„ ν•œλ‹€. μ§€μ—­ λ³€μˆ˜λŠ” ν•¨μˆ˜μ˜ λ…립성을 μ§€μ›ν•˜κ³ , μ „μ—­ λ³€μˆ˜λŠ” λ°μ΄ν„° κ³΅μœ λ₯Ό μš©μ΄ν•˜κ²Œ ν•˜λ©°, μ •μ  λ³€μˆ˜λŠ” λ©”λͺ¨λ¦¬μ˜ νš¨μœ¨μ  μ‚¬μš©κ³Ό λ³€μˆ˜μ˜ μ ‘κ·Ό λ²”μœ„λ₯Ό μ œν•œν•˜λŠ” λ° μ‚¬μš©λœλ‹€. μ΄λŸ¬ν•œ λ³€μˆ˜λ“€μ„ μ μ ˆνžˆ μ‚¬μš©ν•˜λŠ” κ²ƒμ€ ν”„λ‘œκ·Έλž¨μ˜ νš¨μœ¨μ„±κ³Ό μœ μ§€λ³΄μˆ˜μ„±μ„ λ†’μ΄λŠ” λ° κΈ°μ—¬ν•œλ‹€.

728x90
λ°˜μ‘ν˜•
18μ§„μˆ˜