SlideShare a Scribd company logo
Scope Rule & Storage Class
2
Storage Classes
§ Local 변수
– Function안에서 선언되는 variable
– 선언된 function 안에서만 사용 가능
– Function이 호출될 때 function 내부에 선언된 local variable
들은 시스템 스택에 생성되었다가 return하면 없어진다.
• 이후 그 Function이 다시 호출되면, 새로운 영역의 memory가
할당되며, 이전의 값은 유지되지 않는다.
int sum( int k ) {
int a ;
a = k + 3 ;
return a ;
}
3
Storage Classes
§ Local 변수
void func(void) {
int k = 0;
printf( “%dn”, k ) ;
k = 10 ;
printf( “%dn”, k ) ;
}
#include <stdio.h>
void func(void);
int main(void) {
float f = 0 ;
printf( “%fn”, f ) ;
func() ;
func() ;
return 0;
}
4
Storage Classes
§ Global 변수
– Function block 밖에서 선어된 변수
– 모든 함수에서 사용 가능
– 프로그램이 시작할 때 생성된 후 프로그램이 종료될 때까지
메모리에 존재
• 생성된 후 자동으로 0으로 초기화 된다.
int g ;
int main()
{
printf( “%dn”, g );
my_func();
printf( “%dn”, g );
return 0;
}
void my_func()
{
g = 10;
}
5
Storage Classes
§ Global 변수
void func(void) {
int k = 0;
printf( “%d %dn”, k, ++g );
k = 10;
printf( “%d %dn”, k, ++g );
}
#include <stdio.h>
int g;
void func(void);
int main(void) {
printf( “%dn”, g );
func();
printf( “%dn”, g );
return 0;
}
6
Storage Classes
§ static 변수
– global변수와 매우 유사
• 프로그램 시작할 때 생성되었다가 프로그램 끝날 때까지 메모리에
존재
• 변수가 생성된 이후 자동적으로 0으로 초기화가 됨
– Function내부에서 선언된 경우
• 선언된 function 내에서만 접근 가능
– Function 외부에서 선언된 경우
• 선언된 source file내에서만 접근 가능 (추후 여러 파일로 프로그램
작성하기 참고)
7
Storage Classes
§ static 변수
void func(void)
{
static int s ;
int k = 0;
printf( “%d %dn”, k, ++s ) ;
k = 10 ;
printf( “%d %dn”, k, ++s ) ;
}
#include <stdio.h>
void func(void);
int main(void)
{
func() ;
func() ;
return 0;
}
8
Scope Rules
§ static 변수
– 예제: 함수가 몇 번째 호출 되었나?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void do_nothing(void) ;
int main(void)
{
int j ;
srand( time(NULL) ) ;
for( j = 0 ; j < 10 ; j++ )
if( rand() % 2 ) do_nothing() ;
return 0;
}
void do_nothing(void)
{
static int cnt ;
printf( “do_nothing is called %d time(s)n”, ++cnt ) ;
}
9
Storage Classes
§ register 변수
– memory 중에서 가장 빠른 register를 사용하도록 요청
– int만 register로 선언가능
– register로 선언하지 않아도 register에 할당될 수 있고,
register로 선언했어도 register에 할당되지 않을 수 있음
[Ex]
int main() {
register int i; /* register i 와 같음 */
for(i = 0; i < LIMIT; ++i) {
…
}
}
10
const
§ const
– 값을 수정할 수 없는 변수를 선언할 때 type의 앞에 사용
– 선언할 때 반드시 초기화해야 함. 초기화후 값 변경 불 가능
– 변수의 값을 바꿀 수 없도록 선언하는 것.
• 상수를 선언하는 것 아님
[Ex] const int a = 1;
[Ex] const int a = 1;
a = 4; /* error!! */
a++; /* error!! */
11
Scope Rules
§ Scope Rules
– 어느 특정 identifier의 접근 가능성 여부 판단
– declaration statement위치에 따라 접근 범위 결정
– block안에서 선언된 identifier
• 그 block안에서 만 접근 가능
• 그 block이 수행될 때 변수가 생성되었다가 수행이 끝나면 변수
가 없어짐
– block 밖에서 선언된 identifier
• 기본적으로 모든 block에서 접근 가능
• 프로그램이 시작할 때 생겼다가 프로그램이 끝날 때 없어짐.
12
Scope Rules
§ Scope Rules
#include <stdio.h>
int g ;
void function() {
static int a ;
printf(“a=%d g=%d”, a, g);
}
int main(void) {
int a = 3, b = 4;
{
int b=5;
printf(“%d %d”, a, b, g);
}
printf(“%d %d”, a, b, g);
function() ;
}
각각의 변수가 언제 생겼다가
언제 없어지는가?
어느 변수가 어느 변수를
의미하는가?

More Related Content

PPTX
javascript02
PPTX
Startup JavaScript 6 - 함수, 스코프, 클로저
PPTX
Startup JavaScript 3 - 조건문, 반복문, 예외처리
PPTX
Startup JavaScript 4 - 객체
PPTX
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
PDF
2.Startup JavaScript - 연산자
PDF
7가지 동시성 모델 4장
PPTX
7가지 동시성 모델 - 3장. 함수형 프로그래밍
javascript02
Startup JavaScript 6 - 함수, 스코프, 클로저
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 4 - 객체
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
2.Startup JavaScript - 연산자
7가지 동시성 모델 4장
7가지 동시성 모델 - 3장. 함수형 프로그래밍

What's hot (19)

PDF
함수적 사고 2장
PDF
Learning Node Book, Chapter 5
PPTX
11장 윈도우 스레드 풀
PPT
호이스팅, 클로저, IIFE
PDF
[OpenTRS-001] Hotel California
PPTX
Startup JavaScript 8 - NPM, Express.JS
PPTX
Lightning talk - 11
PPTX
tcp ip study
PDF
[Windows via c/c++] 4장 프로세스
PDF
Scope and Closure of JavaScript
PPTX
[Pl in c++] 9. 다형성
PDF
[Algorithm] Shell Sort
PPTX
windows via c++ Ch 5. Job
PDF
PDF
7가지 동시성 모델 - 데이터 병렬성
PPTX
Promise 패턴 공부
PDF
JavaScript Promises
PDF
C 언어 스터디 02 - 제어문, 반복문, 함수
PPTX
함수적 사고 2장
Learning Node Book, Chapter 5
11장 윈도우 스레드 풀
호이스팅, 클로저, IIFE
[OpenTRS-001] Hotel California
Startup JavaScript 8 - NPM, Express.JS
Lightning talk - 11
tcp ip study
[Windows via c/c++] 4장 프로세스
Scope and Closure of JavaScript
[Pl in c++] 9. 다형성
[Algorithm] Shell Sort
windows via c++ Ch 5. Job
7가지 동시성 모델 - 데이터 병렬성
Promise 패턴 공부
JavaScript Promises
C 언어 스터디 02 - 제어문, 반복문, 함수
Ad

Viewers also liked (9)

PDF
2 2. operators
PDF
6. function
PDF
7 mid term summary
PDF
10장 문자열클래스와파일클래스
PDF
7. arrays
PDF
12 1. multi-dimensional array
PDF
11. array & pointer
PDF
2 3. standard io
PDF
15 2. arguement passing to main
2 2. operators
6. function
7 mid term summary
10장 문자열클래스와파일클래스
7. arrays
12 1. multi-dimensional array
11. array & pointer
2 3. standard io
15 2. arguement passing to main
Ad

Similar to 7. variable scope rule,-storage_class (20)

PDF
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
PDF
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
PPTX
C review
PPTX
Lightning talk - 3
PDF
9. pointer
PPTX
[170327 1주차]C언어 A반
PDF
HI-ARC PS 101
PDF
Effective C++ Chapter 1 Summary
PDF
4. stack
PDF
프로그래밍 언어 기초(델파이,C++)
PPTX
Api design for c++ pattern
PDF
2013 C++ Study For Students #1
PPTX
A tour of C++ : the basics
PPTX
Api design for c++ ch3 pattern
PDF
Effective c++ chapter1 2_dcshin
PDF
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
PPTX
Effective cpp
PDF
코딩인카페 C&JAVA 기초과정 C프로그래밍(3)
PPT
Multithread programming 20151206_서진택
PPTX
Effective c++(chapter 5,6)
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
C review
Lightning talk - 3
9. pointer
[170327 1주차]C언어 A반
HI-ARC PS 101
Effective C++ Chapter 1 Summary
4. stack
프로그래밍 언어 기초(델파이,C++)
Api design for c++ pattern
2013 C++ Study For Students #1
A tour of C++ : the basics
Api design for c++ ch3 pattern
Effective c++ chapter1 2_dcshin
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
Effective cpp
코딩인카페 C&JAVA 기초과정 C프로그래밍(3)
Multithread programming 20151206_서진택
Effective c++(chapter 5,6)

More from 웅식 전 (20)

PDF
15 3. modulization
PDF
14. fiile io
PDF
13. structure
PDF
12 2. dynamic allocation
PDF
10. pointer & function
PDF
5 2. string processing
PDF
5 1. character processing
PDF
15 1. enumeration, typedef
PDF
4. loop
PDF
3 2. if statement
PDF
3 1. preprocessor, math, stdlib
PDF
2 3. standard io
PDF
2 1. variables & data types
PDF
Goorm ide 교육용버전 for skku(학생)
PDF
구름 기본 소개자료
PDF
Goorm ide 소개 슬라이드(교육용 버전)
PDF
W14 chap13
PDF
13th chapter12 slide
PDF
Week12 chapter11
PDF
15 3. modulization
14. fiile io
13. structure
12 2. dynamic allocation
10. pointer & function
5 2. string processing
5 1. character processing
15 1. enumeration, typedef
4. loop
3 2. if statement
3 1. preprocessor, math, stdlib
2 3. standard io
2 1. variables & data types
Goorm ide 교육용버전 for skku(학생)
구름 기본 소개자료
Goorm ide 소개 슬라이드(교육용 버전)
W14 chap13
13th chapter12 slide
Week12 chapter11

7. variable scope rule,-storage_class

  • 1. Scope Rule & Storage Class
  • 2. 2 Storage Classes § Local 변수 – Function안에서 선언되는 variable – 선언된 function 안에서만 사용 가능 – Function이 호출될 때 function 내부에 선언된 local variable 들은 시스템 스택에 생성되었다가 return하면 없어진다. • 이후 그 Function이 다시 호출되면, 새로운 영역의 memory가 할당되며, 이전의 값은 유지되지 않는다. int sum( int k ) { int a ; a = k + 3 ; return a ; }
  • 3. 3 Storage Classes § Local 변수 void func(void) { int k = 0; printf( “%dn”, k ) ; k = 10 ; printf( “%dn”, k ) ; } #include <stdio.h> void func(void); int main(void) { float f = 0 ; printf( “%fn”, f ) ; func() ; func() ; return 0; }
  • 4. 4 Storage Classes § Global 변수 – Function block 밖에서 선어된 변수 – 모든 함수에서 사용 가능 – 프로그램이 시작할 때 생성된 후 프로그램이 종료될 때까지 메모리에 존재 • 생성된 후 자동으로 0으로 초기화 된다. int g ; int main() { printf( “%dn”, g ); my_func(); printf( “%dn”, g ); return 0; } void my_func() { g = 10; }
  • 5. 5 Storage Classes § Global 변수 void func(void) { int k = 0; printf( “%d %dn”, k, ++g ); k = 10; printf( “%d %dn”, k, ++g ); } #include <stdio.h> int g; void func(void); int main(void) { printf( “%dn”, g ); func(); printf( “%dn”, g ); return 0; }
  • 6. 6 Storage Classes § static 변수 – global변수와 매우 유사 • 프로그램 시작할 때 생성되었다가 프로그램 끝날 때까지 메모리에 존재 • 변수가 생성된 이후 자동적으로 0으로 초기화가 됨 – Function내부에서 선언된 경우 • 선언된 function 내에서만 접근 가능 – Function 외부에서 선언된 경우 • 선언된 source file내에서만 접근 가능 (추후 여러 파일로 프로그램 작성하기 참고)
  • 7. 7 Storage Classes § static 변수 void func(void) { static int s ; int k = 0; printf( “%d %dn”, k, ++s ) ; k = 10 ; printf( “%d %dn”, k, ++s ) ; } #include <stdio.h> void func(void); int main(void) { func() ; func() ; return 0; }
  • 8. 8 Scope Rules § static 변수 – 예제: 함수가 몇 번째 호출 되었나? #include <stdio.h> #include <stdlib.h> #include <time.h> void do_nothing(void) ; int main(void) { int j ; srand( time(NULL) ) ; for( j = 0 ; j < 10 ; j++ ) if( rand() % 2 ) do_nothing() ; return 0; } void do_nothing(void) { static int cnt ; printf( “do_nothing is called %d time(s)n”, ++cnt ) ; }
  • 9. 9 Storage Classes § register 변수 – memory 중에서 가장 빠른 register를 사용하도록 요청 – int만 register로 선언가능 – register로 선언하지 않아도 register에 할당될 수 있고, register로 선언했어도 register에 할당되지 않을 수 있음 [Ex] int main() { register int i; /* register i 와 같음 */ for(i = 0; i < LIMIT; ++i) { … } }
  • 10. 10 const § const – 값을 수정할 수 없는 변수를 선언할 때 type의 앞에 사용 – 선언할 때 반드시 초기화해야 함. 초기화후 값 변경 불 가능 – 변수의 값을 바꿀 수 없도록 선언하는 것. • 상수를 선언하는 것 아님 [Ex] const int a = 1; [Ex] const int a = 1; a = 4; /* error!! */ a++; /* error!! */
  • 11. 11 Scope Rules § Scope Rules – 어느 특정 identifier의 접근 가능성 여부 판단 – declaration statement위치에 따라 접근 범위 결정 – block안에서 선언된 identifier • 그 block안에서 만 접근 가능 • 그 block이 수행될 때 변수가 생성되었다가 수행이 끝나면 변수 가 없어짐 – block 밖에서 선언된 identifier • 기본적으로 모든 block에서 접근 가능 • 프로그램이 시작할 때 생겼다가 프로그램이 끝날 때 없어짐.
  • 12. 12 Scope Rules § Scope Rules #include <stdio.h> int g ; void function() { static int a ; printf(“a=%d g=%d”, a, g); } int main(void) { int a = 3, b = 4; { int b=5; printf(“%d %d”, a, b, g); } printf(“%d %d”, a, b, g); function() ; } 각각의 변수가 언제 생겼다가 언제 없어지는가? 어느 변수가 어느 변수를 의미하는가?