Posted on 10 October 2010.
Life time and scope:
================
Lets discuss about life time and scope of different variables/elements of your C program.
Life time : This tells about what will be the life time of an element in C program.
Scope : This tells about what parts of program can access a variable/element of a C program.
Lets see what will be the life time and scope of different kinds of variables.
Auto / local variables : Its life time starts from starting of the function where it was declared and ends as soon as
that function ends.
Its scope is with in that function only (that means that variable declared in a function can’t be
accessed out side of that function.)
Global variables : Its life time starts from starting of the program and ends as soon as your program ends.
Its scope is through out that program .
Local static variables: Its life time starts from starting of the function where it was declared and will remain through out that
program.
Its scope is with in that function only (that means that variable declared in a function can’t be
accessed out side of that function.)
Note : This applies as long as you don’t pass pointer to that variable, if you pass the address then you can access
Global static variables : Its life time starts from starting of your program and remains through out the program.
Its scope is with in that file ( you can’t access it from other files of your program)
Note : This also applies as long as you don’t pass pointer to that variable.
Static functions: Its life time starts from starting of your program and remains through out the program.
Its scope is with in that file ( you can’t access it from other files of your program)
Note : This also applies as long as you don’t pass pointer to that function.
Normal functions : Its life time starts from starting of your program and remains through out the program.
Its scope is through out the program ( you can access it from all other files of your program)
In later sessions we will see complete pictorial representation of memory architecture and scope and life time.
Latest Comments