A block of statement in C is surrounded
by curly braces i.e { and }.
We can declare variables inside the
block, such variables called as
local,can be referenced only within the
body of the block.
When the control reaches the opening
brace ,these variables come into
existence and get destroyed as soon as
the control leaves the block through
Click on to see example While(flag) { int x; if(i>x) {
int j; ………..
…….. ………. }
}
1.Scope of
x
is the while block.
2.j
is the if block.
3.The variable
x
can also be referenced to in the if
block because this block is completely enclosed in the
while block.
4.Scope of
j
is only in the if block.
The function scope pertains to the
labels declared in a function in the
sense that a label can be used
Global variables are declared outside all
blocks and functions, they are available
to all the blocks and functions.
The scope of a global variable is in the
entire program file. This type of scope
is known as file scope.
Variables have different type of behavior.
Some variables used in function
sub-program locally, but others execute
globally.
Variables are stored in memory in different
ways by using the function.
Every time execution period (i.e life time)
and boundary(i.e scope and visibility) of
the variable vary in function subprograms.
Every variable in c language has powerful
int qty,rate;
main()
{
int gross,net;
---}
fun()
{
char c;
{
int a;
}
}
Click to see scope rules
If we do not specify
storage class of a
variable compiler
will assume its
storage class as
default i.e
automatic.
Each and every
Storage place of the variable i.e memory
or CPU registers.
The initial value of a variable.
The scope or the visibility of a variable
The life time of a variable i.e how long the
1.
Automatic storage class(with specifier
auto
).
2.
Register storage class(with specifier
register
).
3.
Static storage class(with specifier
static
).
4.
External storage class(with specifier
Storage Memory
Default initial value Garbage Value i.e
unpredictable value
Scope or visibility Local or visible to the block in
which variable is declared eg.if variable is declared in a function it is only visible to that function.
Life Time It retains its value till the
control remains in the block in which it is declared.
main() {
auto int i , j=5; int k , l = 10;
printf(“\n value of i=%d \n value of j= %d”, i , j); printf(“\n value of k = %d \n value of l=%d”,k,l); }
Output:
Value of i = 2009
Value of j=5
main() {
auto int x = 5; {
auto int x = 10; {
auto int x = 15; {
printf(“\n %d”,x); }
printf(“%d”,x); }
printf(“%d”,x); }
}
output:
.
Storage CPU registers
Default initial value Garbage Value i.e
unpredictable value
Scope or visibility Local or visible to the block in
which variable is declared eg.if variable is declared in a
function it is only visible to that function.
Life Time It retains its value till the
control remains in the block in which it is declared.
main()
{
register int i;
for(i=1;i<=100;i++)
{
printf(“\n %d”,i);
}
}
.
Storage Memory
Default initial value zero
Scope or visibility Local or visible to the block in
which variable is declared e.g. if variable is declared in a
function it is only visible to that function.
Life Time It retains its value between
different function calls i.e.
when function is called for the first time, static variable is
created with initial value zero and in subsequent calls it
main() { int i,r; for(i=1;i<=5;i++) { r=tot (i);
printf(“\t %d”,r); }
getch(); }
tot (int x) {
static int s=0; s=s+x;
return (s); }
main() calls the function tot() five times, each time sending the value of i varying from 1 to 5.In the
function value is added to s and the accumulated result is returned.Once this program is executed following result is returned.
output
.
Storage Memory
Default initial value zero
Scope or visibility Scope is local to the block.
Life Time Life is,as long as the program
• Extern storage class for only those variables are used
almost all functions in the program.This would avoid unnecessary passing of the variables.
• If a variable is declared (with global scope) in one file but
referenced in another, the extern keyword is used to inform the compiler of the variable's existence:
In declare.c: int farVar; In use.c:
{ extern int farVar;
int a;
a = farVar * 2; }
• Note that the extern keyword is for declarations, not
definitions.
• An extern declaration does not create any storage; that
int x = 2; /* global variable x */ void f(void) {
float x; /* local variable x to f(), different from global x */ float y;
x = 3.0; /* sets local x declared in this function f() to 3.0 */ /* global x remains the same */
{
int x; /* new local variable x */
x = 4; /* outer x remains values of 3.0 */ }
y = x; /* sets y to 3.0 */ }
/* global x is never modified by anything done in f() */
This method of hiding variables is a potential source of bugs and
int x = 1; /* global variable x */ int main() {
int x = 2; /* local variable x to main */
printf(“local x in outer scope of main is %d\n”, x);
{
int x = 3; /* new local variable x */
printf(“local x in inner scope of main is %d\ n”, x);
}
printf(“local x in outer scope of main is %d\n”, x);
a();
printf(“local x in main is %d\n”, x); }
void a( void) {
int x = 10; /* initialized each time a() is called */
++x;
printf(“local x in a() is %d\n”, x); }
int x = 1; /* global variable x */ int main() {
int x = 2; /* local variable x to main */ printf(“1. local x in main is %d\n”, x);
a(); b(); c(); a(); b(); c();
printf(“2. local x in main is %d\n”, x); }
void a( void) {
int x = 10; /* initialized each time a() is called */ ++x;
printf(“local x in a() is %d\n”, x); }
void b( void) {
static int x = 20; /* initialized on 1st time b() is called. */ ++x;
printf(“local static x in b() is %d\n”, x); } void c( void) {
++x;
printf(“global x in c() is %d\n”, x); }