๐ค static ๋ณ์
๐ค ์ง์ญ๋ณ์ ์ฌ์ฉ

์ง์ญ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋น์ฐํ a๋ 2๋ฒ ๋ค 11์ด๋ค.
๐ค static ๋ณ์ ์ฌ์ฉ

๋ณ์ a๊ฐ static์ด๋ค. ๋ฐ๋ผ์ ์ถ๋ ฅ ๊ฐ์ "11", "12"์ด๋ค.
์ฃผ์ํ ์ ์, ํจ์ ๋ด์์ a๋ฅผ ์ ์ธํ์์๋ ๋ถ๊ตฌํ๊ณ , static์ด์ฌ์ ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋ ๋๊น์ง ๋ฉ๋ชจ๋ฆฌ๊ณต๊ฐ์ ์กด์ฌํ๊ฒ๋๋ค.
#include <stdio.>
void funCount();
int main(void) {
int num;
for(num=0; num<2; num++)
funCount();
return 0;
}
-----------------------
void funCount() {
int num = 0;
static int count;
printf("num=%d, count=%d\n",
++num, count++);
}
#include <stdio.h>
void main() {
int s1,s2;
s1 = funcA(2);
printf("F1 = %d,",s1);
s1 = funcA(3);
printf("F2 = %d,", s1);
s2 = funcB(2);
printf("F3 = %d,", s2);
s2 = funcB(3);
printf("F4 = %d ", s2);
}
------------------------
int funcA(int n) {
static int s = 1;
s *= n;
return s;
}
int funcB(int n) {
int s = 1;
s *= n;
return s;
}
๋ฐ๋ผ์ ๋ชจ๋ ์ถ๋ ฅํ๋ฉด "F1=2, F2=6, F3=2, F4=3" ์ด๋ค.