Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Scope Rules

C Scope Rules

In any programming language, scope is the region in which a variable defined in a program exists; outside that region, the variable cannot be accessed. In C, there are three places where variables can be declared:

  1. inside a function or blocklocalVariables
  2. outside all functionsGlobalVariables
  3. informin the definition of function parameters

Let's take a look at whatlocalvariables,Globalvariables andformparameter was added.

local variables

Variables declared inside a function or block are called local variables. They can only be used by statements inside that function or code block. Local variables are unknown outside the function. The following is an example of using local variables. Here, all the variables a, b, and c are local variables of the main() function.

Example

#include

<

stdio.h

int

main

(

)

{

/*

Local variable declaration

*/

int

a

,

b

;

int

c

;

/*

Actual initialization

*/

a

=

10

;

b

=

20

;

c

=

a

b

;

printf

(

"

value of a = %d, b = %d and c = %d

\

n

"

,

a

,

b

,

c

)

;

return

0

;

}

Global Variables

Global variables are defined outside functions, usually at the top of the program. Global variables remain valid throughout the program's lifecycle and can be accessed from within any function.

Global variables can be accessed by any function. That is, once declared, a global variable is available throughout the entire program. The following is an example of using global and local variables:

Example

#include

<

stdio.h

/*

Global variable declaration

*/

int

g

;

int

main

(

)

{

/*

Local variable declaration

*/

int

a

,

b

;

/*

Actual initialization

*/

a

=

10

;

b

=

20

;

g

=

a

b

;

printf

(

"

value of a = %d, b = %d and g = %d

\

n

"

,

a

,

b

,

g

)

;

return

0

;

}

In a program, a local variable and a global variable can have the same name, but inside a function, if the two names are the same, the local variable value is used and the global variable is not. Here is an example:

In a program, the local and global variables'

Example

#include

<

stdio.h

/*

Global variable declaration

*/

int

g

=

20

;

int

main

(

)

{

/*

Local variable declaration

*/

int

g

=

10

;

printf

(

"

value of g = %d

\

n

"

,

g

)

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



value of g = 10

formal parameters

Function parameters, formal parameters, are treated as local variables within that function; if they have the same name as a global variable, they take precedence. Here is an example:

Example

#include

<

stdio.h

/*

Global variable declaration

*/

int

a

=

20

;

int

main

(

)

{

/*

Local variable declarations in the main function

*/

int

a

=

10

;

int

b

=

20

;

int

c

=

0

;

int

sum

(

int

,

int

)

;

printf

(

"

value of a in main() = %d

\

n

"

,

a

)

;

c

=

sum

(

a

,

b

)

;

printf

(

"

value of c in main() = %d

\

n

"

,

c

)

;

return

0

;

}

/*

A function that adds two integers

*/

int

sum

(

int

a

,

int

b

)

{

printf

(

"

value of a in sum() = %d

\

n

"

,

a

)

;

printf

(

"

value of b in sum() = %d

\

n

"

,

b

)

;

return

a

b

;

}

When the code above is compiled and executed, it produces the following result:



value of a in main() = 10

value of a in sum() = 10

value of b in sum() = 20

value of c in main() = 30

The difference between global and local variables in memory:

  • Global variables are stored in the global storage area of memory and occupy static storage units;
  • Local variables are stored on the stack, and storage units are dynamically allocated to the variable only when the function it belongs to is called.
  • For more information, refer to:The use of static in C/C++: global and local variables

    Initializing Local and Global Variables

    When a local variable is defined, the system does not initialize it; you must initialize it yourself. When a global variable is defined, the system initializes it automatically, as shown below:

    Data Type Default Initialization Values
    int 0
    char '\0'
    float 0
    double 0
    pointer NULL

    Properly initializing variables is a good programming habit; otherwise, the program may sometimes produce unexpected results, because uninitialized variables lead to garbage values already present in the memory location.