By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,334 Members | 690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,334 IT Pros & Developers. It's quick & easy.

Regarding Static varable in C/C++/Java

P: 1
HI friends...

Can any body give reason / logic behind static varable lifetime through out the process execution.

i.e how does compiler maintains the static varable life through out the process execution ?


example

#include<stdio.h>

void func1( );

main()
{
.......
for (i=0;i<5;i++)
func1( );
.........
return 0;
}


finc1( )
{
.......
static int i=0; /* next time the control comes here this statement has no effect on "i" why n how ?*/
printf("Static int i=%d\n",i++);
.......
}


the o/p is 0 to 4...

I will be thankfull for replies....
Jan 8 '07 #1
Share this Question
Share on Google+
2 Replies

vpawizard
P: 66
In the simplest terms, when a variable is declared as static (as in your example), the variable is visible only within the function but acts like a global variable,in the sense that it mantains its value throughout the lifetime of process.

Regards,
Jan 8 '07 #2

10K+
P: 13,262
HI friends...

Can any body give reason / logic behind static varable lifetime through out the process execution.

i.e how does compiler maintains the static varable life through out the process execution ?


example

#include<stdio.h>

void func1( );

main()
{
.......
for (i=0;i<5;i++)
func1( );
.........
return 0;
}


finc1( )
{
.......
static int i=0; /* next time the control comes here this statement has no effect on "i" why n how ?*/
printf("Static int i=%d\n",i++);
.......
}


the o/p is 0 to 4...

I will be thankfull for replies....
The meaning of static in C++ and in java is not exactly the same.

1.)When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified.

2.)When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).A variable declared static in a function retains its state between calls to that function.
3.)When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. (This is also true for Java).

4.)When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.(Also true for Java).
Static data members of classes must be initialized at file scope.

For a more detailed discussion of static keyword in java, have a look the Java classes page on this site page.
Jan 8 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.