By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Order of function call

P: n/a

Hi All,
Please help me to understand the reason behind the output of the
following program

What is the output of the following program:
int i=10;
int f1()
{
static int i = 15;
printf("f1:%d ", i);
return i--;
}
main()
{
int i, j;
i = 5;
printf("%d %d %d", f1(), f1(), i);
}

OUTPUT is

f1:15 f1:14 14 15 5
How is it possible ? Is it not be f1:15 f1:14 14 13 5

Regards,
Somenath

Feb 19 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Feb 19, 6:31 pm, "somenath" <somenath...@gmail.comwrote:
Hi All,
Please help me to understand the reason behind the output of the
following program

What is the output of the following program:
int i=10;
int f1()
{
static int i = 15;
printf("f1:%d ", i);
return i--;}

main()
{
int i, j;
i = 5;
printf("%d %d %d", f1(), f1(), i);

}

OUTPUT is

f1:15 f1:14 14 15 5
How is it possible ? Is it not be f1:15 f1:14 14 13 5

Regards,
Somenath
The output can be different with different compilers.
The language does not specify the order in which the function
arguments are evaluated. Kindly refer to the page 53 in K & R 2nd
edition.

Feb 19 '07 #2

P: n/a
In article <11**********************@k78g2000cwa.googlegroups .com>,
somenath <so*********@gmail.comwrote:
>What is the output of the following program:
int i=10;
int f1()
{
static int i = 15;
printf("f1:%d ", i);
return i--;
}
main()
{
int i, j;
i = 5;
printf("%d %d %d", f1(), f1(), i);
}

OUTPUT is

f1:15 f1:14 14 15 5
How is it possible ? Is it not be f1:15 f1:14 14 13 5
Where does the 13 come from? f1() returns the old value of i. As to
the order, the arguments to a function may be evaluated in any order.
Many implementations evaluate them in right-to-left order, because
that's the order they want to put them on the stack, but the standard
doesn't guarantee anything about it.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 19 '07 #3

P: n/a
somenath wrote:
Hi All,
Please help me to understand the reason behind the output of the
following program

What is the output of the following program:
The output -- in fact, the entire behavior of the
program -- is undefined, because the program calls the
printf() function without a correct declaration in scope.
Even if you get away with that error, there may be no
output at all because the program does not generate a
complete line ending with a '\n' character. And even if
it generates output, the host environment may do something
nasty with it when the program exits without supplying a
valid exit status. In what follows, I'll assume the
indicated corrections have been made:

#include <stdio.h /* so printf() will be declared */
int i=10;
int f1()
{
static int i = 15;
printf("f1:%d ", i);
return i--;
}
main()
int main(void)
{
int i, j;
i = 5;
printf("%d %d %d", f1(), f1(), i);
printf("%d %d %d\n", f1(), f1(), i); /* ending the line */
return 0; /* valid exit status */
}

OUTPUT is

f1:15 f1:14 14 15 5
Either the undefined behavior has caught up with you,
or the C implementation you use is hopelessly broken, or
you have made an inaccurate report. The "output" you show
has fewer space characters than your printf() formats should
produce.

Now, why am I making such a fuss over a little bit of
white space? Because it indicates that you almost certainly
typed the so-called "output" free-hand, and did not cut and
paste or otherwise make a perfect copy of the actual output.
This in turn casts doubt on the accuracy of your transcription
of the program source: How many differences are there between
what you've showed us and what you actually ran? I can only
guess -- and if I guess wrong, it's *your* fault.

In the future, make *exact* copies of code, output, error
messages, and whatever else you're asking about. Otherwise,
the answers you get may have little to do with the problem
you face.

The two possible outputs of the (corrected) program are

f1:15 f1:14 15 14 5
f1:15 f1:14 14 15 5

.... depending on the order in which the printf() inside main()
evaluates its arguments. C does not specify that order, so
different implementations may do it differently: left-to-right,
right-to-left, or even more peculiar orderings are possible.
In your case, it appears that the arguments may have been
evaluated right-to-left.
How is it possible ? Is it not be f1:15 f1:14 14 13 5
No: That output is not possible (even allowing for the
changes in white space, and assuming a corrected program).
Perhaps you misunderstand how `--i' and `i--' are different;
a session with your textbook may be helpful.

--
Eric Sosman
es*****@acm-dot-org.invalid

Feb 19 '07 #4

P: n/a
In article <er***********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.ukwrote:
....
>Many implementations evaluate them in right-to-left order, because
that's the order they want to put them on the stack, but the standard
doesn't guarantee anything about it.
You are, of course, not allowed to use the word "stack" in this
newsgroup, and, within the hour, one or more of the nitpicking twits
will be calling you on it.

Another word you are not allowed to use in this ng is "cdecl", but, if
you are willing to specify the calling convention that your function
uses, and you specify it as "cdecl", then you get RTL evaluation.

Of course, the hallowed C standard says nothing about "calling conventions"
either, so those are probably also words you cannot use in this ng w/o
getting hate postings from various exhibitors of Aspergers-like symptoms.

Feb 19 '07 #5

P: n/a
Kenny McCormack wrote:
[his usual asininity]
Please do not feed the trolls.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 19 '07 #6

P: n/a
In article <7J******************************@comcast.com>,
Eric Sosman <es*****@acm-dot-org.invalidwrote:
>Kenny McCormack wrote:
>[his usual insightful wisdom]

Please do not feed the trolls.
You're telling *me* not to feed myself???

Feb 19 '07 #7

P: n/a
Kenny McCormack said:
In article <7J******************************@comcast.com>,
Eric Sosman <es*****@acm-dot-org.invalidwrote:
>>Kenny McCormack wrote:
>>[his usual insightful wisdom]

Please do not feed the trolls.

You're telling *me* not to feed myself???
Yeah. Well, one never knows. It *might* work. After a few weeks or so,
anyway.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 19 '07 #8

P: n/a
somenath wrote:
>
Please help me to understand the reason behind the output of the
following program

What is the output of the following program:
int i=10;
int f1()
{
static int i = 15;
printf("f1:%d ", i);
return i--;
}
main()
{
int i, j;
i = 5;
printf("%d %d %d", f1(), f1(), i);
}
It is undefined. Primarily due to the use of printf (a variadic
function) without a prototype. There are other errors or foolish
omissions.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 20 '07 #9

P: n/a
Richard Heathfield wrote:
>
Kenny McCormack said:
>Eric Sosman <es*****@acm-dot-org.invalidwrote:
>>Kenny McCormack wrote:

[his usual insightful wisdom]

Please do not feed the trolls.

You're telling *me* not to feed myself???

Yeah. Well, one never knows. It *might* work. After a few weeks
or so, anyway.
With luck it will go around and around in ever decreasing circles,
until it disappears up its own asshole.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 20 '07 #10

P: n/a
On Feb 19, 9:10 am, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
Another word you are not allowed to use in this ng is "cdecl", but, if
you are willing to specify the calling convention that your function
uses, and you specify it as "cdecl", then you get RTL evaluation.

While the platforms I've seen implement cdecl use it to indicate that
the parameters are stored on the callers stack in something
approximating the traditional right-to-left order, I've never actually
seen that extended to include a definition of the actual order of
evaluation of those parameters.

Feb 21 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.