473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regarding Static varable in C/C++/Java

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
2 1597
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
r035198x
13,262 8TB
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

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

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
2
by: Petra Neumann | last post by:
Hello, I just came across some sample code where a variable has been defined as: static public final int TABLE = 1, LIST = 2; I was wondering if there is a difference to writing public static...
3
by: Ron_Adam | last post by:
Hi, Sometimes it just helps to see what's going on, so I've been trying to write a tool to examine what names are pointing to what objects in the current scope. This still has some glitches,...
4
by: raghavendra | last post by:
Hi , A static member can be accessed only by another static method....but the vice-versa is not true....Can anyone pls explain me the logic behind this... Also in a project, if we have too many...
2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
9
by: sonu | last post by:
Hi All, Pls clarify me what is the difference between static member and static method in c. pls some one reply me with Example.
1
by: Gary | last post by:
Open the web browser at the same time, the session varable in the second broswer is disappeared. All The Session Varable is declared in the webform page load.
10
by: pradeep84 | last post by:
Hi.. to all.... i hav compiled the following program... the buttons in the program not performing there actions.... i don't know why... plz help me to resolve this problem... Thanks...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.