Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Order of evaluation of function arguments

Reply
Thread Tools

Order of evaluation of function arguments

 
 
Kenneth Brody
Guest
Posts: n/a
 
      12-23-2005
Keith Thompson wrote:
>
> Niklas Norrthon <(E-Mail Removed)> writes:
> > "dragoncoder" <(E-Mail Removed)> writes:
> >
> >> Consider the following code.
> >>
> >> #include <stdio.h>
> >>
> >> int main()
> >> {
> >> int i =1;
> >> printf("%d ,%d ,%d\n",i,++i,i++);
> >> return 0;
> >> }
> >>
> >> Is the above code well defined? If yes, what is the output. If no, why
> >> ?

> >
> > Read the FAQ!

>
> The FAQ doesn't directly address this. Question 3.2 discusses
> i++ * ++i, but it doesn't explicitly mention modifying the same
> variable twice in different arguments of the same function call.
> Combine this with the common misconception that the commas separating
> arguments are comma operators, and the confusion is understandable.
>
> It would be good for question 3.2 to mention function arguments. On
> the other hand, I see that <http://www.eskimo.com/~scs/C-faq/q3.2.html>
> celebrated its tenth birthday two months ago (that's ten years since
> the page has been modified).


Perhaps simply modifying 3.8 to include:

Note that, although the comma operator is a sequence point, the
comma used to separate function parameters is not the comma
operator, and therefore is not a sequence point.

Of course, to confuse things, one could combine the comma operator
with the comma separator:

printf("%d, %d, %d\n", i, (j++, j++), k);

Is this guaranteed to increment j twice, passing the singly-imcremented
value to printf?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-23-2005
Kenneth Brody <(E-Mail Removed)> writes:
[...]
> Of course, to confuse things, one could combine the comma operator
> with the comma separator:
>
> printf("%d, %d, %d\n", i, (j++, j++), k);
>
> Is this guaranteed to increment j twice, passing the singly-imcremented
> value to printf?


Yes.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Order of evaluation of arguments in a subroutine Krishna Chaitanya Perl Misc 1 12-09-2010 07:49 PM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
order of evaluation of arguments to constructors Peter C++ 7 03-22-2007 11:09 PM
evaluation of arguments to a function Greenhorn C Programming 7 03-04-2005 11:37 AM
order of evaluation for method arguments Michael Garriss Ruby 6 09-13-2003 01:20 PM



Advertisments