Scope of variables
1- Global Scope:
It is declared outside any function and can be used by later blocks of code.
So, they are declared outside " main() ", always above it.
So, they are declared outside " main() ", always above it.
Global variables get memory at compile time. They are saved in the "Data segment".
Data segment is divided into two parts: " .data and .bss "
Data segment is divided into two parts: " .data and .bss "
.data : contains the initialized ( with a non-zero value ) global and static local variables.
.bss : contains the uninitialized ( or initialized to zero ) global and static local variables.
Default value for uninitialized global variables is "zero". At load time, the uninitialized global variables will be put in ".bss" and initialized to "zero".
.bss : contains the uninitialized ( or initialized to zero ) global and static local variables.
Default value for uninitialized global variables is "zero". At load time, the uninitialized global variables will be put in ".bss" and initialized to "zero".
2- Scope inside blocks (Local scope):
Local variables are declared inside blocks. Blocks are any number of statements surrounded by curly brackets "{}" .
Blocks inherit all variables from global scope.
You can't declare variables with the same name in the same scope, but you can have variables with the same name but with different scopes.
So, you can have global variable and local variable with the same name. But in this case, the local variable will hide (but not replace) the global variable.
In C, there is no way to access global variable from a block if there is a local variable to this block with the same name of the global variable. (note: solution of this in C++ is done by "name spaces")
So, you can have global variable and local variable with the same name. But in this case, the local variable will hide (but not replace) the global variable.
In C, there is no way to access global variable from a block if there is a local variable to this block with the same name of the global variable. (note: solution of this in C++ is done by "name spaces")
This method of variables hiding is a potential source of bugs, so avoid making it.
Default value of uninitialized local variables is " garbage " .
Local variables take memory at run time and they are stored in the segment stack.
Ex:
#include <stdio.h>
#include <stdint.h>
uint8_t x_g; //Uninitialized global variable, default value " zero"
uint8_t x_g; //Uninitialized global variable, default value " zero"
void main()
{ //the start of the block
uint8_t x_g; //Uninitialized local variable, default value
//is garbage.Same name with global variable so
//it hides it.
uint8_t x_L; //Uninitialized local variable, default value
//is garbage.
x_g = 5; //This assigns this value to x_g which is local to
//main(), and this is the meaning hiding, as you are
//seeing the local variable to your block.
printf("Value of x_g = %d \n", x_g);
printf("Value of x_L = %d \n", x_L);
} //end of the block
Output: Value of x_g = 5
Value of x_l = (garbage)
Value of x_l = (garbage)
No comments:
Post a Comment