473,411 Members | 2,030 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,411 software developers and data experts.

static question

I know that a variable declared static at the beginning of a function will
stick around until the program terminates, e.g.:

void func(void)
{
static foo;
..
}

But does the same apply to variables which are declared static inside a
sub-function block, e.g.

void func(void)
{
while (..) {
static foo;
}
}

I think 'foo' only lasts while the loop is executed, but I am not sure.
I mean, it would be kind of confusing if it were that way..


Jan 1 '08 #1
3 1247
"copx" <co**@gazeta.plwrites:
I know that a variable declared static at the beginning of a function will
stick around until the program terminates, e.g.:

void func(void)
{
static foo;
..
}

But does the same apply to variables which are declared static inside a
sub-function block, e.g.
Yes.
void func(void)
{
while (..) {
static foo;
}
}

I think 'foo' only lasts while the loop is executed, but I am not
sure.
The name is only visible inside the block, but the variable exists
for the whole execution of the program.
I mean, it would be kind of confusing if it were that way..
It would. If your second 'foo' came and went as execution enters
leaves the block, it would be an automatic variable -- i.e. just like
a normal one declared without 'static'.

You are using implicit int. 'static int foo;' is better.

--
Ben.
Jan 1 '08 #2
On Tue, 01 Jan 2008 12:26:47 +0100, copx wrote:
I know that a variable declared static at the beginning of a function
will stick around until the program terminates, e.g.:

void func(void)
{
static foo;
..
}

But does the same apply to variables which are declared static inside a
sub-function block, e.g.

void func(void)
{
while (..) {
static foo;
}
}
Yes, the same applies.

It should be noted that you specify no explicit type for foo. It is
better to write this:

static int foo;

--
C
Jan 1 '08 #3

"Citizen C" <ci******@spamcop.netschrieb im Newsbeitrag
news:aQ***********@newsfe02.lga...
On Tue, 01 Jan 2008 12:26:47 +0100, copx wrote:
>I know that a variable declared static at the beginning of a function
will stick around until the program terminates, e.g.:

void func(void)
{
static foo;
..
}

But does the same apply to variables which are declared static inside a
sub-function block, e.g.

void func(void)
{
while (..) {
static foo;
}
}

Yes, the same applies.
Thanks everyone!

Jan 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
10
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
9
by: Pohihihi | last post by:
What could be the possible reasons (technical/non technical) of not using lots of static functions or variables in a program keeping in mind that Framework by itself has tons of static functions and...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
10
by: Franky | last post by:
I think I misread a post and understood that if I do: System.Windows.Forms.Cursor.Current = Cursors.WaitCursor there is no need to reset the cursor to Default. So I made all the reset...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.