C spot run: initializing variables

K

Keith

I've got this bit of code, and I can't really explain to myself why it does what it does....

int
func1()
{
static int r=0;
return(++r);
}

int
func2()
{
static int r;
r = 0;
return(++r);
}

int
main()
{
printf("%d,%d,%d\n", func1(), func1(), func1());
printf("%d,%d,%d\n", func2(), func2(), func2());
exit(0);
}


I imagined it would print
1,1,1
1,1,1

but after compiling and running, it prints
3,2,1
1,1,1

In func1, are the static r and the returned r not the same var? what's what?

Even it the returned r is a global, why does it display 3,2,1 instead of 1,2,3?
 
B

Bill Pursell

Keith said:
I've got this bit of code, and I can't really explain to myself why it does what it does....

int
func1()
{
static int r=0;
return(++r);
}

int
func2()
{
static int r;
r = 0;
return(++r);
}

int
main()
{
printf("%d,%d,%d\n", func1(), func1(), func1());
printf("%d,%d,%d\n", func2(), func2(), func2());
exit(0);
}


I imagined it would print
1,1,1
1,1,1

but after compiling and running, it prints
3,2,1
1,1,1

In func1, are the static r and the returned r not the same var? what's what?
Even it the returned r is a global, why does it display 3,2,1 instead of 1,2,3?

In func1, that initialization setting r to zero occurs only
once, and the value is retained for each call. In func2, the
value is retained, but you are setting it to zero each time
you call it. When you call
printf("%d,%d,%d\n", func1(), func1(), func1());
the calls to the functions may be made in any order.
In your case, you're seeing them called in reverse order,
with the final call being made first. Another implementation
might print "2,3,1" for that line.
 
P

pete

Keith said:
static int r=0;

.... means exactly the same thing as:
static int r;

All static object definitions
without explicit initializations,
are default initialized as though by
= {0}
All static initialization happens
before main starts executing.
 
L

lovecreatesbeauty

Keith said:
int
func2()
{
static int r;
r = 0;

every time set r to 0.
int
main()
{
printf("%d,%d,%d\n", func1(), func1(), func1());

The are no sequence points among three arguments, so the order of
evaluation of these three arguments is undefined. The program result is
undefined, it maybe has so many choices:

printf("%d,%d,%d\n", func1(), func1(), func1());

Round #1 func1(): 1
func1(): 2
func1(): 3
For this sequence the program prints: 1,2,3


Round #2
func1(): 1
func1(): 2
func1(): 3
For this sequence the program prints: 3,2,1

....

Round #6
....
 
P

pete

lovecreatesbeauty said:
The are no sequence points among three arguments, so the order of
evaluation of these three arguments is undefined.
The program result is
undefined, it maybe has so many choices:

No.
This situation is a little different.
Function calls are sequence points.
The result is merely unspecified.
 

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
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top