CHAPTER 9
PROGRAM STRUCTURE

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
Our previous programs have had a simple structure. This chapter deals with more complex program organizations. We finish up our description of variables local to particular functions and introduce variables accessible by any function. While discussing variables, we present the storage classes that control their lifetime, location, and visibility. We then show how header files simplify accessing variables and functions defined in other files, and how we can have variables and functions accessible only within a single file. This chapter concludes with a case study: an initial implementation of a set data type.

Jump to: [Previous Chapter | Next Chapter]


  1. LOCAL VARIABLES
  2. GLOBAL VARIABLES
  3. STORAGE CLASSES
  4. TYPE QUALIFIERS
  5. USER-DEFINED TYPES
  6. HEADER FILES
  7. PRIVATE VARIABLES AND FUNCTIONS
  8. CASE STUDY: ABSTRACT DATA TYPES

Objectives





¿¹)

main module: main functionÀ» Æ÷ÇÔÇÏ´Â ÇÁ·Î±×·¥ÀÇ ±âº» ¸ðµâ

graphics module: graphics °ü·Ã functionµé·Î ±¸¼ºµÈ ÇÁ·Î±×·¥ fileµé

communication module, text processing module, ...


                              main.c
                              ----------------------
                              #include "module1.h"
                              #include "module2.h"

                              main()
                              {
                                   ProcA();
                                   ProcB();
                              }


module1.h                                                    module2.h
--------------                                               ---------------
void ProcA(void);                                            void ProcB(void);


module1.c                                                    module2.c
--------------                                               ---------------
#include "module1.h"                                         #include "module2.h"

void ProcA(void)                                             void ProcB(void)
{                                                            {
. . .                                                        . . .
}                                                            }





º¯¼öÀÇ »ç¿ë ¹üÀ§ (¿µÇâÀ» ¹ÌÄ¡´Â ¹üÀ§, scope), ¼ö¸í




Global variable (Àü¿ª º¯¼ö): Function ¿ÜºÎ¿¡¼­ Á¤ÀÇµÇ¾î ±× ÈÄ ¸ðµç function¿¡¼­ »ç¿ë °¡´É. ÇÁ·Î±×·¥ ¼öÇà ½Ã °è¼Ó Á¸Àç (¸Þ¸ð¸®¸¦ ÇÒ´ç¹Þ¾Æ ÇÁ·Î±×·¥ Á¾·á ½Ã±îÁö »ç¿ë °¡´É)



Local variable (Áö¿ª º¯¼ö): Function ³»ºÎ ¶Ç´Â block ³»¿¡¼­ Á¤ÀÇµÇ¾î ±× function ¶Ç´Â block ³»¿¡¼­¸¸ »ç¿ë °¡´É. Function ½ÇÇàÀÌ ³¡³ª¸é ÀÚµ¿ ¼Ò¸ê.


¿¹)

int g;              // global variable g declaration

void func(void)
{
   int i;           // local variable i ¼±¾ð

   . . . 
}



Storage class - ÀÚ·á°¡ À¯ÁöµÇ¾î¾ß ÇÒ ±â°£ ¹× ¹üÀ§ °áÁ¤


Storage class Á¾·ù

  1. auto: automatic, default storage class (Ưº°ÇÑ Á¤ÀÇ°¡ ¾øÀ¸¸é ÀÚµ¿À¸·Î auto·Î ÁöÁ¤)

  2. static: ±â¾ï Àå¼Ò¸¦ ¿µ±¸È÷ ÇÒ´ç

  3. extern: external, ´Ù¸¥ file¿¡¼­ Á¤ÀÇµÈ global variable °øÀ¯

  4. register: °¡´ÉÇÑ ÇÑ CPU register ÀÌ¿ë (°í¼Ó ¼öÇàÀÌ °¡´ÉÇÏ´Ù). ÄÄÇ»ÅÍ ±âÁ¾¿¡ µû¶ó »ç¿ë Á¦ÇÑ


¿¹)

static int g;    // º¯¼ö g´Â ÀÌ º¯¼ö°¡ ¼±¾ðµÈ file ³»¿¡¼­¸¸ »ç¿ë °¡´É
float f;         // º¯¼ö f´Â ´Ù¸¥ file¿¡¼­µµ ÂüÁ¶ °¡´É.
                 // ´Ü, extern float f; ¸¦ ¼±¾ðÇØ ÁÖ¾î¾ß ÇÑ´Ù.

void func1(void) // Function func1Àº ´Ù¸¥ file¿¡¼­µµ È£Ãâ °¡´É.
                 // ´Ü, function prototypeÀ» Á¤ÀÇÇØ ÁÖ¾î¾ß ÇÑ´Ù.
{
   static int i; // º¯¼ö i´Â func1 ½ÇÇàÀÌ ³¡³ªµµ °ªÀ» À¯Áö.
   int d;        // º¯¼ö d´Â func1 ½ÇÇàÀÌ ³¡³ª¸é °ªÀ̼Ҹê
   . . .
}

static func2(void) // Function func2´Â ´Ù¸¥ file¿¡¼­ È£Ãâ ºÒ°¡´É
{
   . . .
}



º¯¼öÀÇ ÃʱâÈ­ (Variable Initialization)

  • º¯¼ö´Â »ç¿ëÇϱâ Àü¿¡ ¹Ýµå½Ã ÃʱâÈ­ÇÏ¿©¾ß ÇÑ´Ù.

  • ÃʱâÈ­ÇÏÁö ¾ÊÀº auto¿Í register º¯¼ö´Â º¯¼ö¿¡ ÇÒ´çµÈ ¸Þ¸ð¸®¿¡ ÀúÀåµÈ ÀÓÀÇÀÇ °ªÀ» »ç¿ëÇÑ´Ù.

  • ÃʱâÈ­ÇÏÁö ¾ÊÀº global°ú static º¯¼ö´Â 0À¸·Î compile½Ã ÃʱâÈ­µÈ´Ù.


    º¯¼ö ÃʱâÈ­ ¿¹)

  • auto¿Í register º¯¼ö ÃʱâÈ­: [ÀÚ·áÇü º¯¼ö¸í = ½Ä(expression)]
              int a = 10;
              char ch = 'A';
              int b = a + ch;
    


  • global°ú static º¯¼ö ÃʱâÈ­: [ÀÚ·áÇü º¯¼ö¸í = ¼ö½Ä(numeric expression)]
              static int a = 10;
              static double d = 3.141592;
              int b = 10 * 20;
              int c = a * b;     // Wrong
              int d = getchar(); // Wrong
    

    ¿©·¯ °³ÀÇ ¼­·Î ¿¬°üµÈ ÀÚ·á¿¡ ´ëÇØ Æ¯Á¤ °ªÀ» ºÎ¿©ÇÏ·Á¸é

    ¹æ¹ý 1) #define ¹® »ç¿ë

    #define SUNDAY 0
    #define MONDAY 1
    #define TUESDAY 2
    #define WEDNESDAY 3
    #define THURSDAY 4
    #define FRIDAY 5
    #define SATURDAY 6

    ¹æ¹ý 2) Enumeration type »ç¿ë

    enum { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } ;

    enum typeÀÇ Á¤ÀÇ : enum tag_name { list of elements };
    enum variable Á¤ÀÇ : enum tag_name list of variables ;
    enum tag_name { list of elements } list of variables
    ;

  • °¢°¢ÀÇ element¿¡ ƯÁ¤°ªÀ» ºÎ¿©ÇÒ ¼ö ÀÖ°í ƯÁ¤ °ªÀ» ºÎ¿©ÇÏÁö ¾ÊÀ» °æ¿ì ù¹ø° element¿¡ 0À» ¹èÁ¤ÇÏ°í ´ÙÀ½ element ºÎÅÍ 1 ¾¿ Áõ°¡.

    ¿¹)

    enum MONTH { JAN = 1, FEB, MAR, ..., NOV, DEC };
    enum MONTH BirthMonth, SalaryMonth, ... ;


    Enumeration type°ú °°ÀÌ Çϳª, µÑ, ¼¿ ¼ö ÀÖ´Â ÇüÅÂÀÇ ÀڷḦ scalar typeÀ̶ó ÇÑ´Ù. C¿¡¼­ scalar typeµéÀº ÀÚµ¿ÀûÀ¸·Î integer·Î 󸮵ȴÙ. ±×·¯¹Ç·Î enum typeÀÇ elementµéµµ ¿¬»ê¿¡ »ç¿ëµÉ ¼ö ÀÖ´Ù. (±×·¯³ª »ó¼ö·Î Ãë±ÞµÇ±â ¶§¹®¿¡ »õ·Î¿î °ªÀ» assignÇÒ ¼ö ¾ø´Ù.)


    Type Qualifiers : const, volatile

    const : ƯÁ¤ º¯¼ö¸¦ »ó¼öó·³ Ãë±Þ. º¯¼ö °ªÀÌ ÃʱâÈ­ µÈ ÈÄ °ª º¯°æÀÌ ºÒ°¡´É.

    ¿¹) const int MAXVAL = 100;
    #define MAXVAL 100 °úÀÇ Â÷ÀÌ´Â?


    »õ·Î¿î data typeÀÇ Á¤ÀÇ : typedef

    typedef data_structure type_name;

    ¿¹)

    typedef unsigned char BYTE;
    BYTE a, b, c;
    typedef enum { FALSE, TRUE } bool; bool x, y, z;
    typedef char * string; typedef enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } WEEK_DAY ;


    Interface Design (Random Number Library)

    Algorithms


    ½Ç½À) ´ÙÀ½°ú °°Àº »ê¼ö ±³À°¿ë ÇÁ·Î±×·¥À» ÀÛ¼ºÇ϶ó.

    ÀÓÀÇÀÇ µÎ ¼öÀÇ ´õÇϱ⠶Ǵ »©±â¸¦ ¿¬½ÀÇÑ´Ù. (´õÇϱ⠶Ǵ »©±â¸¦ ÀÓÀÇ·Î ¼±ÅÃ)

    »ç¿ë ¿¹)

    °è»êÇØ º¸¼¼¿ä. 23 + 5 ? 28
    ¸Â¾ÒÀ¾´Ï´Ù. Âü ÀßÇß¾î¿ä.

    °è»êÇØ º¸¼¼¿ä. 35 - 17 ? 20
    ´Ù½Ã ÇØ º¸¼¼¿ä. 35 - 17 ? 18
    ¸Â¾ÒÀ¾´Ï´Ù. Âü ÀßÇß¾î¿ä.

    °è»êÇØ º¸¼¼¿ä. 17 - 20 ? 20
    ´Ù½Ã ÇØ º¸¼¼¿ä. 17 - 20 ? 18
    ´Ù½Ã ÇØ º¸¼¼¿ä. 17 - 20 ? 0
    ´Ù½Ã ÇØ º¸¼¼¿ä. 17 - 20 ? -1
    ´Ù½Ã ÇØ º¸¼¼¿ä. 17 - 20 ? -3
    ¸Â¾ÒÀ¾´Ï´Ù. Âü ÀßÇß¾î¿ä.

    °è»êÇØ º¸¼¼¿ä. 35 - 17 ? [ctrl-z]
    Á¾·á


    Assignment #7 (±â°£: 1ÁÖÀÏ)

    À§ ÇÁ·Î±×·¥À» ÀúÇгâ¿ëÀ¸·Î ´ÙÀ½°ú °°ÀÌ ¼öÁ¤ º¸¿ÏÇ϶ó.

    10Àå (Pointer) ÀÐ¾î ¿À±â


    [ Table Of Contents | Previous Chapter | Next Chapter]