Develop
2003.04.23 10:56

[c] 스토리지 클래스(변수)

Views 8197 Votes 0 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
변수의 스코프(scope): 변수가 프로그램내에서 사용될 수 있는 범위
변수의 라이프타임(lifetime): 변수가 존재하는 시간(기간)
스토리지 클래스(storage class): 스토리지 클래스는 변수의 스코프와 라이프타임을 결정한다.

스토리지 클래스 종류
1. extern : 전역변수 
        스코프: 프로그램 전체
        라이프타임: 프로그램이 실행되는 모든 시간

2. auto: 지역 변수
        스코프: 변수가 선언된 블락이나 함수
        라이프타임: 변수가 선언된 함수나 블락이 실행되는 동안

3. static: 정적 변수
        스코프: 변수가 선언된 블락이나 함수
        라이프타임: 프로그램이 실행되는 모든 시간 동안

4. register: 레지스터 변수, 프로그램의 최적화를 위해 사용된다.
        스코프: auto와 동일
        라이프타임: auto와 동일

*변수는 정의되기 전에 사용될 수 없다.

예제: storage.c
     1  #include <stdio.h>
     2
     3  int  extern_int = 10;
     4
     5  int add(int a, int b) {
     6      auto    int  auto_int;
     7      static  int  static_int;
     8
     9      auto_int = a + b + extern_int;
    10      static_int +=  auto_int;
    11      printf("tadd::auto_int = %dn", auto_int);
    12      printf("tadd::static_int = %dn", static_int);
    13      return auto_int;
    14  }
    15
    16  main(int argc, char ** argv) {
    17      register  int  i, sum = 0;
    18      auto      int  d;
    19
    20      for(i=0; i < 5; i++) {
    21          d = i*i;
    22          extern_int++;
    23          sum += add(i, d);
    24          printf("n");
    25      }
    26      printf("sum = %d n", sum);
    27  }

결과
#7 enterprise:/data1/student/c9844000% storage
        add::auto_int = 11
        add::static_int = 11

        add::auto_int = 14
        add::static_int = 25

        add::auto_int = 19
        add::static_int = 44

        add::auto_int = 26
        add::static_int = 70

        add::auto_int = 35
        add::static_int = 105

sum = 105 


예제: storage2.c
     1  #include <stdio.h>
     2
     3  int  value = 10;
     4
     5  main(int argc, char ** argv) {
     6      register  int  i;
     7
     8      printf("value = %dn", value);
     9      for(i=0; i < 5; i++) {
    10          int value = 20;
    11          if(i == 2) {
    12              int value = 30;
    13              printf("ttvalue = %dn", value);
    14          } else {
    15              printf("tvalue = %dn", value);
    16          }
    17      }
    18      printf("value = %dn", value);
    19  }


결과
#15 enterprise:/data1/student/c9844000% storage2
value = 10
        value = 20
        value = 20
                value = 30
        value = 20
        value = 20
value = 10

예제: storage3.c
     1  #include <stdio.h>
     2
     3  int  value = 10;
     4
     5  main(int argc, char ** argv) {
     6      register  int  i;
     7
     8      printf("value = %dttt:&value = %pn", value, &value);
     9      for(i=0; i < 5; i++) {
    10          int value = 20;
    11          if(i == 2) {
    12              int value = 30;
    13              printf("ttvalue = %dt:&value = %pn", value, &value);
    14          } else {
    15              printf("ttvalue = %dt:&value = %pn", value, &value);
    16          }
    17      }
    18      printf("value = %dttt:&value = %pn", value, &value);
    19  }

결과
#22 enterprise:/data1/student/c9844000% storage3
value = 10                      :&value = 20b08
                value = 20      :&value = effffa38
                value = 20      :&value = effffa38
                value = 30      :&value = effffa34
                value = 20      :&value = effffa38
                value = 20      :&value = effffa38
value = 10                      :&value = 20b08


예제: storage4.c
     1  #include <stdio.h>
     2
     3  int  value = 10;
     4
     5  main(int argc, char ** argv) {
     6      register  int  i;
     7
     8      printf("value = %dttt:&value = %pn", value, &value);
     9      for(i=0; i < 5; i++) {
    10          static int value = 20;
    11          if(i == 2) {
    12              int value = 30;
    13              printf("ttvalue = %dt:&value = %pn", value, &value);
    14          } else {
    15              printf("ttvalue = %dt:&value = %pn", value, &value);
    16          }
    17      }
    18      printf("value = %dttt:&value = %pn", value, &value);
    19  }

결과
#28 enterprise:/data1/student/c9844000% storage4
value = 10                      :&value = 20b10
                value = 20      :&value = 20b14
                value = 20      :&value = 20b14
                value = 30      :&value = effffa38
                value = 20      :&value = 20b14
                value = 20      :&value = 20b14
value = 10                      :&value = 20b10


예제: storage5.c
     1  #include <stdio.h>
     2
     3  int  value = 10;
     4
     5  main(int argc, char ** argv) {
     6      register  int  i, sum = 0;
     7
     8      printf("sum = %dttt:&sum = %pn", sum, &sum);
     9      printf("value = %dttt:&value = %pn", value, &value);
    10      for(i=0; i < 5; i++) {
    11          static int value = 20;
    12          if(i == 2) {
    13              int value = 30;
    14              printf("ttvalue = %dt:&value = %pn", value, &value);
    15          } else {
    16              printf("ttvalue = %dt:&value = %pn", value, &value);
    17          }
    18      }
    19      printf("value = %dttt:&value = %pn", value, &value);
    20  }

컴파일
#32 enterprise:/data1/student/c9844000% cc -o storage5 storage5.c
"storage5.c", line 8: cannot take address of register: sum

#34 enterprise:/data1/student/c9844000% gcc -o storage5 storage5.c
storage5.c: In function `main':
storage5.c:8: warning: address of register variable `sum' requested


예제: storage6.c
     1  #include <stdio.h>
     2
     3  int  extern_int = 10;
     4  extern int add(int, int);
     5
     6  main(int argc, char ** argv) {
     7      register  int  i, sum = 0;
     8      auto      int  d;
     9
    10      for(i=0; i < 5; i++) {
    11          d = i*i;
    12          extern_int++;
    13          sum += add(i, d);
    14          printf("n");
    15      }
    16      printf("sum = %d n", sum);
    17  }
    18
    19  int add(int a, int b) {
    20      auto    int  auto_int;
    21      static  int  static_int;
    22
    23      auto_int = a + b + extern_int;
    24      static_int +=  auto_int;
    25      printf("tadd::auto_int = %dn", auto_int);
    26      printf("tadd::static_int = %dn", static_int);
    27      return auto_int;
    28  }

결과
#37 enterprise:/data1/student/c9844000% storage6
        add::auto_int = 11
        add::static_int = 11

        add::auto_int = 14
        add::static_int = 25

        add::auto_int = 19
        add::static_int = 44

        add::auto_int = 26
        add::static_int = 70

        add::auto_int = 35
        add::static_int = 105

sum = 105 


예제: storage7.c 
     1  #include <stdio.h>
     2
     3  int  value = 10;
     4
     5  main(int argc, char ** argv) {
     6      register  int  i;
     7      int       value = 20;
     8
     9      printf("value = %dn", value);
    10      for(i=0; i < 5; i++) {
    11          extern int value;
    12          if(i == 2) {
    13              int value = 30;
    14              printf("ttvalue = %dn", value);
    15          } else {
    16              printf("tvalue = %dn", value);
    17          }
    18      }
    19      printf("value = %dn", value);
    20  }

결과
#45 enterprise:/data1/student/c9844000% storage7
value = 20
        value = 10
        value = 10
                value = 30
        value = 10
        value = 10
value = 20

?

  1. [c] 단기과정[01/17] 후위연산 스택 계산기..

    Date2003.04.23 CategoryDevelop Byhooni Views7226
    Read More
  2. [c] 단기과정[01/24] 정렬 알고리즘

    Date2003.04.23 CategoryDevelop Byhooni Views6955
    Read More
  3. [js] 새로고침(refresh)방법과 다른 페이지 바꾸기..

    Date2003.04.23 CategoryDevelop Byhooni Views6521
    Read More
  4. [vbs] CD롬 뱉는 스크립트..

    Date2003.04.23 CategoryDevelop Byhooni Views11699
    Read More
  5. [php] 초간단 게시판 페이지 분할 알고리즘

    Date2003.04.23 CategoryDevelop Byhooni Views8699
    Read More
  6. [c] C에서 MySQL DB 사용하기~

    Date2003.04.23 CategoryDevelop Byhooni Views7011
    Read More
  7. [jsp][php] LDAP 프로그래밍..

    Date2003.04.23 CategoryDevelop Byhooni Views7386
    Read More
  8. [php] 초간단 웹 카운터..

    Date2003.04.23 CategoryDevelop Byhooni Views7063
    Read More
  9. [c] 프로그래밍 과제..(colprint)

    Date2003.04.23 CategoryDevelop Byhooni Views6745
    Read More
  10. [java] 스윙(swing) 인터페이스 이용해서 만든 구구단.. ㅋㅋ

    Date2003.04.23 CategoryDevelop Byhooni Views6682
    Read More
  11. [c++] Window API(MFC) 오목 게임

    Date2003.04.23 CategoryDevelop Byhooni Views10275
    Read More
  12. [js] 가운데 새창 뜨는 함수와 이벤트

    Date2003.04.23 CategoryDevelop Byhooni Views6722
    Read More
  13. [php] 메모장 - 웅지학원 ([c] mysql 백업프로그램 포함)

    Date2003.04.23 CategoryDevelop Byhooni Views6993
    Read More
  14. [c] 문자열 컨트롤 함수로 만든 프로그램들..

    Date2003.04.23 CategoryDevelop Byhooni Views6786
    Read More
  15. [js] 주민번호 생성기..

    Date2003.04.23 CategoryDevelop Byhooni Views8142
    Read More
  16. [c] 문자열 라이브러리 최신버전

    Date2003.04.23 CategoryDevelop Byhooni Views7188
    Read More
Board Pagination Prev 1 ... 7 8 9 10 11 ... 53 Next
/ 53