![[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]](../Images/QuarterFrontCover.bmp) 
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]
¿¹)
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)
{                                                            {
. . .                                                        . . .
}                                                            }
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 ¼±¾ð
   . . . 
}
¿¹)
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¿¡¼ È£Ãâ ºÒ°¡´É
{
   . . .
}
º¯¼ö ÃʱâÈ ¿¹)
          int a = 10;
          char ch = 'A';
          int b = a + ch;
          static int a = 10;
          static double d = 3.141592;
          int b = 10 * 20;
          int c = a * b;     // Wrong
          int d = getchar(); // Wrong
#define SUNDAY 0 #define MONDAY 1 #define TUESDAY 2 #define WEDNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6
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
;
¿¹)
enum MONTH { JAN = 1, FEB, MAR, ..., NOV, DEC };
enum MONTH BirthMonth, SalaryMonth, ... ;
Enumeration type°ú °°ÀÌ Çϳª, µÑ, ¼¿ ¼ö ÀÖ´Â ÇüÅÂÀÇ ÀڷḦ scalar typeÀ̶ó ÇÑ´Ù. C¿¡¼ scalar typeµéÀº ÀÚµ¿ÀûÀ¸·Î integer·Î 󸮵ȴÙ. ±×·¯¹Ç·Î enum typeÀÇ elementµéµµ ¿¬»ê¿¡ »ç¿ëµÉ ¼ö ÀÖ´Ù. (±×·¯³ª »ó¼ö·Î Ãë±ÞµÇ±â ¶§¹®¿¡ »õ·Î¿î °ªÀ» assignÇÒ ¼ö ¾ø´Ù.)
const : ƯÁ¤ º¯¼ö¸¦ »ó¼öó·³ Ãë±Þ. º¯¼ö °ªÀÌ ÃʱâÈ µÈ ÈÄ °ª º¯°æÀÌ ºÒ°¡´É.
¿¹) const int MAXVAL = 100;
#define MAXVAL 100 °úÀÇ Â÷ÀÌ´Â?
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 ;
ÀÓÀÇÀÇ µÎ ¼öÀÇ ´õÇϱ⠶Ǵ »©±â¸¦ ¿¬½ÀÇÑ´Ù. (´õÇϱ⠶Ǵ »©±â¸¦ ÀÓÀÇ·Î ¼±ÅÃ)
»ç¿ë ¿¹)
°è»êÇØ º¸¼¼¿ä. 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]
Á¾·á
À§ ÇÁ·Î±×·¥À» ÀúÇгâ¿ëÀ¸·Î ´ÙÀ½°ú °°ÀÌ ¼öÁ¤ º¸¿ÏÇ϶ó.