STATTIC Storage Class IN C

Status
Not open for further replies.

sacrpio

Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
Dear friends,

#include<stdio.h>

static int a=1;
void main()
{
static int a=2;
fun();
printf("%d", a);
}

fun()
{
static int a=3;
}

In above example at how many places in data area variable a will be stored.
 

Hi,
I think it should be 3 places as each are static and need to be existent through out the life time of the program.
 

3 different independent variables;
look at this

Code:
#include <stdio.h> 

static int a=1;
void main()
{
static int a=2;
fun();
fun2();
printf("\n%d", a);
}

fun()
{
static int a=3;
printf( "\n%d",a);
}

fun2()
{
printf("\n%d", a);

}


now look at this

Code:
#include <stdio.h> 

static int a=1;
void main()
{
static int a=2;
fun();
fun2();
printf( "\n%lx",&a);
}

fun()
{
static int a=3;
printf( "\n%lx",&a);
}

fun2()
{
printf("\n%lx", &a);

}
 

Static is also used for scope control. Declaring a global variable static limits it's scope to that file, equaly, declaring a function prototype and function static limits it's scope to that file. You can use this to implement Namespaces.
Who needs C++?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…