static 변수


static 변수

지역변수에 static 선언 추가하기 - 선언된 함수 내에서만 접근이 가능 (지역변수 특성) - 딱 1회 초기화화되고 프로그램 종료 시까지 메모리 공간에 존재 (전역변수 특성) #include <stdio.h> void SimpleFunc(void) { static int num1 = 0; // 초기화하지 않으면 0 초기화 int num2 = 0; // 초기화하지 않으면 쓰레기 값 초기화 num1++, num2++; printf("static: %d, local: %d \n", num1, num2); } int main(void) { int i; for (i = 0; i < 3; i++) SimpleFunc(); return 0; } static: 1, local: 1 static: 2, local: 1 static: 3, local: 1 static int num1 = 0; 를 살펴보면 아래와 같다. (전역변수 특성) 초기화하지 않으면 전역변수처럼 0으로 초기화된다. 프로그램 시작...


#c언어 #static #static변수

원문링크 : static 변수