446,148 Members | 1,282 Online
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
10 Replies

 P: n/a On Feb 19, 6:31 pm, "somenath"

 P: n/a In article <11**********************@k78g2000cwa.googlegroups .com>, somenath 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 isf1:15 f1:14 14 15 5How 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

 P: n/a In article , Richard Tobin Many implementations evaluate them in right-to-left order, becausethat's the order they want to put them on the stack, but the standarddoesn'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 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 >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. -- "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 >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. -- "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.