다음 C 프로그램의 출력 결과는?
#include <stdio.h>
int accumulate(int x) {
static int total = 1;
total += x;
return total;
}
int main(void) {
int a = accumulate(2);
int b = accumulate(a);
int c = accumulate(-2);
printf("%d %d", b, c);
return 0;
}정답 3번
static total은 호출 사이에 유지된다. 첫 호출에서 1+2=3이 a가 되고, 두 번째 호출에서 total 3에 a 3을 더해 b=6이 된다. 세 번째 호출에서 6-2=4이므로 `6 4`를 출력한다.
콘텐츠 정보
- 자료 유형
- 정처LAB 자체 제작
- 검수 상태
- 내용 검수 완료
- 해설 작성·검수
- 정처LAB 편집 기준
- 최종 검수
- 2026-08-16