June 2005


C/C++Administrator on 30 Jun 2005 03:01 pm

Generally, a global variable, int i, in file a.c can be use in file b.c by, extern int i. But, a static global variable, static int i, in file a.c can’t be used in any other file. So, if a global variable is declared static it can only be used in the file it exists.

How the C compiler work? If a global variable be declared, the compiler will use its name as a symbol. When other files declare extern by the same name, complier will find the symbol and reference this variable. If a static global variable be declared, the compiler will not create a symbol, it will only give this variable an address and all reference in the same file will be replced by this address. When other files declare extern varible by the same name, they can’t find the symbol and make declaring failure.

Thanks Frank offers the information.

IT NewsAdministrator on 28 Jun 2005 10:06 am
SYSTEMAdministrator on 28 Jun 2005 09:39 am
Life NoteAdministrator on 17 Jun 2005 06:05 pm
Life NoteAdministrator on 17 Jun 2005 04:47 pm
Life NoteAdministrator on 13 Jun 2005 04:30 pm
SYSTEMAdministrator on 08 Jun 2005 12:28 am