Home > Back-end >  C language global variable problem, bosses for help
C language global variable problem, bosses for help

Time:01-25

In defining a global variable, if not written storage categories, such as defined in the external int a; That is a default static or extern? Global variables to store category default? Why?

CodePudding user response:

Someone I'm dizzy

CodePudding user response:

See default is static before was not so sure

CodePudding user response:

Global variables by default and static and extern is different,
So there was no global variables default storage categories,

CodePudding user response:

The global position of the
Int a; Allocate space to establish a global variable is initialized to zero, and generously allowed other files to use this variable,
Static int a; Allocate space to establish a global variable is initialized to zero, and other file sharing this variable,

Extern int a; This appeared in the "other files", does not allocate space to establish variable, but the first line of the above variables is introduced into this file to play a role,

CodePudding user response:

Global variables are global variables, the modifier extern use before the variable or function of the statement, used to indicate "this variable/function is defined elsewhere, to quote here" other extern file can be used to refer to the same variable, the static means static storage, at the beginning that the file is available, used in the code block can only be visible in the code block, and the block of code is the variable difference between stack, stack variables declared in a clearing, such as the while int variable declarations within a cycle can be recreated, and the static variable values, and will not eliminate, recreating, program exists static variables,

CodePudding user response:

reference 4 floor forever74 response:

of global positionInt a; Allocate space to establish a global variable is initialized to zero, and generously allowed other files to use this variable,
Static int a; Allocate space to establish a global variable is initialized to zero, and other file sharing this variable,

Extern int a; This appeared in the "other files", does not allocate space to establish variable, but the first line of the above variables is introduced into this file to play a role,

I see, thank you brother
  • Related