A scope in any programming is a region of the program where a defined
variable can have its existence and beyond that variable can not be
accessed. There are three places where variables can be declared in C
programming language:
A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:
- Inside a function or a block which is called local variables,
- Outside of all functions which is called global variables.
- In the definition of function parameters which is called formal parameters.
Local Variables
Variables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. Following is the example using local variables. Here all the variables a, b and c are local to main() function.package main import "fmt" func main() { /* local variable declaration */ var a, b, c int /* actual initialization */ a = 10 b = 20 c = a + b fmt.Printf ("value of a = %d, b = %d and c = %d\n", a, b, c) }When the above code is compiled and executed, it produces the following result:
value of a = 10, b = 20 and c = 30
Global Variables
Global variables are defined outside of a function, usually on top of the program. The global variables will hold their value throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is the example using global and local variables:
package main import "fmt" /* global variable declaration */ var g int func main() { /* local variable declaration */ var a, b int /* actual initialization */ a = 10 b = 20 g = a + b fmt.Printf("value of a = %d, b = %d and g = %d\n", a, b, g) }When the above code is compiled and executed, it produces the following result:
value of a = 10, b = 20 and g = 30A program can have same name for local and global variables but value of local variable inside a function will take preference. Following is an example:
package main import "fmt" /* global variable declaration */ var g int = 20 func main() { /* local variable declaration */ var g int = 10 fmt.Printf ("value of g = %d\n", g) }When the above code is compiled and executed, it produces the following result:
value of g = 10
Formal Parameters
Function parameters, formal parameters, are treated as local variables with-in that function and they will take preference over the global variables. Following is an example:package main import "fmt" /* global variable declaration */ var a int = 20; func main() { /* local variable declaration in main function */ var a int = 10 var b int = 20 var c int = 0 fmt.Printf("value of a in main() = %d\n", a); c = sum( a, b); fmt.Printf("value of c in main() = %d\n", c); } /* function to add two integers */ func sum(a, b int) int { fmt.Printf("value of a in sum() = %d\n", a); fmt.Printf("value of b in sum() = %d\n", b); return a + b; }When the above code 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
Initializing Local and Global Variables
When a local variable as Global variables are initialized to their corresponding 0 value. Pointer is initialized to nil.Data Type | Initial Default Value |
---|---|
int | 0 |
float32 | 0 |
pointer | nil |
No comments:
Post a Comment