티스토리 뷰

Coding/C

static 및 extern // case02

빠리빵 2018. 5. 20. 12:15
#include 
#include 

int a = 10;
static int s = 20;


void f1(void);

void main(void) {
	static int s;  // s = 0이 자동으로 된다. static.

	a++, s++;  // 1. a = 11, s = 1
	printf("%d %d\n", a, s);  
	f1();
	a++, s++;
	printf("%d %d\n", a, s);  // 3. a = 12, s = 2
	f1();
	system("pause");

}

void f1(void) {
	int a = 1;
	static int s = 2;

	a++, s++;  // 2. a =  2, s = 3    
                         // 4. a = 2, s = 4, a는 static이 아니기 때문에 2번째 호출 시 다시 a = 1부터 시작함을 주의
	printf("%d %d\n", a, s);
}

출력 
11 1

2 3

12 2

2 4 

과연 static 변수는 어디에 저장되길래 이러한 결과가 나올 수 있는 것일까... 

다음에 추가하도록. 함수, 변수 자체의 저장 위치와 원리..



'Coding > C' 카테고리의 다른 글

little endian과 big endian  (0) 2018.05.22
포인터의 연산  (0) 2018.05.22
pointer 와 선치, 후치 연산자  (0) 2018.05.20
static 및 extern // case03  (0) 2018.05.20
static 및 extern // case01  (0) 2018.05.20
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함