473,606 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order of function call


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
10 1578
On Feb 19, 6:31 pm, "somenath" <somenath...@gm ail.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
In article <11************ **********@k78g 2000cwa.googleg roups.com>,
somenath <so*********@gm ail.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

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 19 '07 #3
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
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
Kenny McCormack wrote:
[his usual asininity]
Please do not feed the trolls.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 19 '07 #6
In article <7J************ *************** ***@comcast.com >,
Eric Sosman <es*****@acm-dot-org.invalidwrot e:
>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
Kenny McCormack said:
In article <7J************ *************** ***@comcast.com >,
Eric Sosman <es*****@acm-dot-org.invalidwrot e:
>>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
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.securityfoc us.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
Richard Heathfield wrote:
>
Kenny McCormack said:
>Eric Sosman <es*****@acm-dot-org.invalidwrot e:
>>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.securityfoc us.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

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

Similar topics

0
1722
by: Christos TZOTZIOY Georgiou | last post by:
Hi all, this post contains at the end a handy module that I've used quite often when I wanted to analyse the occasional complex expression and how it was to be evaluated. The function analyse_expression is called with a single string argument containing an expression. Names are allowed (actually, preferred over numbers ;-), since the function eval's in a protected dictionary, where names are generated as needed.
7
3651
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
16
697
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following possibility and because the languages do not specify the order of evaluation, doing so was an error. int B::f ( int i, int j = i + 1 ) { // j defaults to i + 1
6
1436
by: lig | last post by:
hi , i am trying to do something that is not working (cuz i tried that) so i hope for some ideas about it. ok, so i have this recursive function, lets call it recFun and i wanted to be able to call it in a specific order. in a simple way : evaluating some expression in its original order see the following part of code:
4
2570
by: Frank Wallingford | last post by:
Note: For those with instant reactions, this is NOT the common "why is i = i++ not defined?" question. Please read on. I came across an interesting question when talking with my colleagues. According to the standard, most operators evaluate their operands in an unspecified order. This means that in code like this: f() + g()
1
4647
by: Thomas Schoen | last post by:
Hi, is it possible to use a parameter of a plpgsql-function to order a selection inside the function? What i would like to do is pass a column-name/alias-name to a plpgsql function and use the variable as order-by-parameter like this: create function foo(varchar) RETURNS SETOF test AS '
21
4103
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
16
2435
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? -> array2 is assigned to array1 ....???? the reason being it is
10
3307
by: nachch | last post by:
Does the C specification define the order of evaluation of assignment statements? For example, what should be the output from the following: int foo1() { printf("foo1\n"); return 0; } int foo2() { printf("foo2\n"); return 0; } int foo3() { printf("foo3\n"); return 0; } int main()
1
3521
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
0
8439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8430
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8094
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6770
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5966
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5465
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
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
0
1296
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.