C Language, Variable Scope and Memory Allocation
1. º¯¼ö ¿µ¿ª(scope)
local: ƯÁ¤ ÇÔ¼ö, ºí·Ï ³»¿¡¼ Á¤ÀÇµÇ°í »ç¿ëµÊ. ÀÚµ¿À¸·Î ÃʱⰩÀÌ ÁöÁ¤µÇÁö ¾ÊÀ½.
global: ¸ðµç ÇÔ¼ö ¹Û¿¡¼ Á¤ÀǵÊ. º¸Åë ÄÚµåÀÇ Ã¹¸Ó¸® preprocessor directive ´ÙÀ½¿¡ À§Ä¡½ÃŲ´Ù. Global º¯¼ö·Î ÁöÁ¤µÈ º¯¼ö(¹®ÀÚ)¸¦ ƯÁ¤ ÇÔ¼ö¿¡¼ ´Ù¸¥ °ªÀ¸·Î º¯°æÇÏ¿© ÁöÁ¤(ÃʱâÈ, assign)ÇÒ ¼ö ÀÖ´Ù. Äڵ忡¼ ÃʱⰪÀÌ ÁöÁ¤µÇÁö ¾ÊÀ¸¸é ÀÚµ¿À¸·Î Á¤¼ö¿Í ½Ç¼ö´Â 0À¸·Î, char´Â \0À¸·Î, Æ÷ÀÎÅÍ´Â NULL·Î ÁöÁ¤µÊ.
formal
parameters: ÇÔ¼öº¯¼ö
int sum(int a, int b){return
a+b;}
[memory
stack: ³ôÀº ÁÖ¼Ò°ªºÎÅÍ °Å²Ù·Î »ç¿ëÇÏ¿© ³»·Á ¿È(¼øÂ÷Àû). Àӽà º¯¼ö ÀúÀå. °ªÀÌ Áö¼ÓÀûÀ¸·Î À¯Áö ¾È µÊ.
heap: µ¿Àû º¯¼ö ÀúÀå. ºñ¼øÂ÷ÀûÀ¸·Î ¸Þ¸ð¸® ÇÒ´ç/ÇØÁ¦. ÇÒ´ç ¼Óµµ ´À¸².
static: Á¤Àû µ¥ÀÌÅÍ ¿µ¿ª. ³·Àº ÁÖ¼Ò °ªºÎÅÍ »ç¿ë. (.data, .bss). °è¼ÓÀûÀ¸·Î °ªÀÌ À¯ÁöµÇ´Â º¯¼ö
[variables, scope
auto: Áö¿ª º¯¼ö. ÇÔ¼ö(ºí·Ï) ¾È¿¡¼¸¸ À¯È¿. »ç¿ë ÈÄ ¼Ò¸ê. ½ºÅà ¸Þ¸ð¸®¿¡ ÇÒ´ç. C¿¡¼´Â auto Ű¿öµå »ý·«µÇ¾î ÀÖÀ½ (Ç¥½Ã ¾øÀ¸¸é auto¿¡ ÇØ´ç)
static: Á¤Àû º¯¼ö. ÇÔ¼ö(ºí·Ï) ¾È¿¡¼¸¸ À¯È¿. ÇÔ¼ö·Î µÇµ¹¾Æ ¿ÔÀ» ¶§ °ªÀÌ À¯ÁöµÊ. ÃʱâÈ »ý·«µÇ¸é ÀÚµ¿À¸·Î 0. static memory¿¡ ÇÒ´ç.
³»ºÎ Á¤Àûº¯¼ö: ÇÔ¼ö ³»ºÎ¿¡¼ ¼±¾ðµÇ¸é ÇÔ¼ö ¾È¿¡¼¸¸ À¯È¿
¿ÜºÎ Á¤Àûº¯¼ö: ÇÔ¼ö ¿ÜºÎ¿¡¼ ¼±¾ð. ÇØ´ç ÆÄÀÏ ³»ºÎ¿¡¼ Àü¿ªº¯¼öó·³ »ç¿ëµÊ.
extern: ÇÔ¼ö ¹Û Àü¿ª ¹üÀ§¿¡¼ ¼±¾ðµÊ. ÇÁ·Î±×·¥ Àüü¿¡¼ À¯È¿ÇÏ°í ´Ù¸¥ ÆÄÀÏ¿¡¼µµ ÂüÁ¶ °¡´É. ÃʱâÈ »ý·« °¡´É. Á¤Àû µ¥ÀÌÅÍ ¿µ¿ª¿¡ ÇÒ´ç. extern º¯¼ö¸¦ ³²¹ßÇϸé ÇÁ·Î±×·¥ º¹Àâ À¯Áö º¸¼ö °ï¶õ. Tells the compiler that this data is defined
somewhere and will be connected with the linker. The program you're building
consists of multiple source files linked together, where some of the variables
defined, for example, in source file file1.c need to be referenced in other
source files, such as file2.c.
