473,507 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C spot run: initializing variables

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?

--
forever clueless
Keith
May 21 '06 #1
4 1533
Keith wrote:
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.

May 21 '06 #2
Keith wrote:
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.

--
pete
May 21 '06 #3

Keith wrote:
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
....

--
lovecreatesbeauty

May 21 '06 #4
lovecreatesbeauty wrote:
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:


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

--
pete
May 21 '06 #5

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

Similar topics

2
1501
by: Dave | last post by:
Hello all, I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member...
17
5591
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
1
417
by: Andreas Boehm | last post by:
Hi *.*, does the standard meanwhile define something about initializing variables by the compiler? I think, it is a side-effect of the OS used, if undefined global (static) variables are...
0
1105
by: Adam Smith | last post by:
Are there any drawbacks to initializing static member variables for classes used in an asp.net application within application_start? My classes have arraylist like tables, which cache information...
17
2581
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
6
3353
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
1892
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
8
7496
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
5
2547
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
10
1875
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
0
7223
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
7319
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,...
0
7376
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
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.