473,569 Members | 2,813 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 1541
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
....

--
lovecreatesbeau ty

May 21 '06 #4
lovecreatesbeau ty 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
1502
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 variable might be used or unused in a given object. For each field, there is a FieldIsValid() member function that must be called before accessing or...
17
5607
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with...
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 initialized to zero. What does the standard define for this? thanx Andreas
0
1107
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 so that I don't have to hit the database every call. I would like to initialize these tables through calling a static class function, Init(), in...
17
2597
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 main(String args) { MyType aMember; ... aMember = new MyType(...) ... } However, in C++ this does not seem to work. I declare in class (it's
6
3358
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
1895
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
7500
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, like this: for(var i=0; i<list; i++) { var name = list; }
5
2550
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 use to run a UM program, I kept on getting error messages. I have used someone else's implementation and it runs fine. I have compared my code...
10
1881
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 it's easy to solve: I just make my own default constructor. Problem: My own default constructor is considered to be *initializing the variable*...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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 we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.