static variable in for loop init

P

paulw

Hi

I have a question:

main() { static int i;

printf("%d\n",i); // should I see see 0 or 5 ???

for (i=5;i<=15;i++) {...} // What's the meaning of static
variable in
// for's init section???
}
 
R

Richard Bos


Note that in C99, implicit int is no longer legal; in that version of
the language, you have to write int main(). int main(void) is better
yet.
static int i;

printf("%d\n",i); // should I see see 0 or 5 ???

Wherever do you get the idea that you _could_ see 5? There's nothing
that gives i any value other than the default 0 that all static objects
have.
for (i=5;i<=15;i++) {...} // What's the meaning of static
variable in
// for's init section???

None whatsoever. The only differences, from the programmer's point of
view, between a static and an automatic object are:
1. Statics are initialised to 0; automatic variables start out
containing garbage unless you explicitly initialise them. This is
relevant to the printf() above, since that relies on i _having_ a value,
and not containing garbage; but it is of no importance to the for loop,
since the first thing you do is give it another value.
2. Statics exist throughout the program's run, and retain their value
between calls of the function they're in; automatics are destroyed and
recreated every time their declarations are encountered, and get their
initial value (or garbage) each time. Since i is in main(), and you
don't call main() recursively, this is of no importance to your program;
even if you did, it still wouldn't mean a thing to the for loop, since,
again, you give i a new value first thing you do in the loop.

Richard
 
J

Jens.Toerring

I have a question:
main() { static int i;

main() is a function that always returns an int and that takes
either 0 or 2 arguments. Since in your case main() doesn't seem
to take an argument better make that

int main( void )
printf("%d\n",i); // should I see see 0 or 5 ???

You will see "0" since static variables are always initialized
to 0 at program startup.
for (i=5;i<=15;i++) {...} // What's the meaning of static
variable in
// for's init section???

It has no special meaning, it's just a variable that has declared
as static. And that doesn't seem to make to much sense here - local
static variables are useful if you need to retain the value of a
variable between invocations of a function, but since you probably
won't call main() recursively and you initilize it at the start of
the loop anyway having the variable declared as static doesn't make
any difference.

BTW, C++-stype comments (the ones starting with a "//") can get
you in trouble, only compilers following the newer C99 standard
must understand them. And when posting code better use the "old"
form of comments since, as you see, line wraping can happen and
then it's much harder to simply copy and paste the program for
testing purposes.
Regards, Jens
 
M

Martin Ambuhl

Hi
I have a question:
main() { static int i;
printf("%d\n",i); // should I see see 0 or 5 ???
for (i=5;i<=15;i++) {...} // What's the meaning of static
variable in
// for's init section???
}

Note that uncompilable code is not a good idea. Let's start with this
version of your code
[1]
#include <stdio.h>

int main(void)
{
static int i;

printf("%d\n", i);
for (i = 5; i <= 15; i++)
printf("%d%c", i, (i == 15) ? '\n' : ' ');
return 0;
}

The difference between this and
[2]
#include <stdio.h>

int main(void)
{
int i;

printf("%d\n", i);
for (i = 5; i <= 15; i++)
printf("%d%c", i, (i == 15) ? '\n' : ' ');
return 0;
}

is that in [1] i is initialized (to 0) and in [2] it is used
uninitialized in the first printf statement. Static variables are
initialized by default (although initialization by default is probably a
poor programming practice). So [1] has well defined output:
0
5 6 7 8 9 10 11 12 13 14 15
while [2], using an uninitialized variable, does not.

The example here does not really show why a static variable might be
used. Variables within a block without the 'static' are 'auto'
(automatic) variables. Each entry to the block has a logically new
variable to work with. Static variables are an implementation of what
in Algol were called 'own' variables; they persist from one use of the
block to the next. Here's an example that I hope will show the difference:

#include <stdio.h>

void auto_i(void)
{
int i = 0;
printf("In 'auto_i', i = %d\n", i);
i++;
}

void static_i(void)
{
static int i = 0;
printf("In 'static_i', i = %d\n", i);
i++;
}

int main(void)
{
int j;
for (j = 0; j < 4; j++) {
auto_i();
static_i();
}
return 0;
}

[output]
In 'auto_i', i = 0
In 'static_i', i = 0
In 'auto_i', i = 0
In 'static_i', i = 1
In 'auto_i', i = 0
In 'static_i', i = 2
In 'auto_i', i = 0
In 'static_i', i = 3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top