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

 
 
dragoncoder
Guest
Posts: n/a
 
      12-21-2005
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
?

Thanks.

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      12-21-2005
dragoncoder wrote:
> 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
> ?


This seems to be a homework question. If yes, please state so
and show us your best shot at answering the question.
If no, convince us.

Otherwise: Read the comp.lang.c FAQ. Apart from that being the
obvious and polite thing to do _before_ posting for the first
time, it should help you answering these question

> Thanks.


You're welcome.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      12-21-2005

dragoncoder wrote:
> 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
> ?
>
> Thanks.


http://www.eskimo.com/~scs/C-faq/q3.2.html

-David

 
Reply With Quote
 
dragoncoder
Guest
Posts: n/a
 
      12-21-2005
This is not a homework question. I just wanted to make it short. I
understand that modifying the value more than once between 2 sequence
points invokes undefined behaviour as mentioned in
http://www.eskimo.com/~scs/C-faq/q3.2.html but here scenario is
different I believe.

I think the comma (,) operator is a sequence point and therefore the
sequence point requirement of UB is not satisfied. I feel it has to do
something with the order of evaluation of argements.

Please put me to right direction.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-21-2005
"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;
> }


See <http://www.eskimo.com/~scs/C-faq/faq.html>, question 3.2.
It applies to function arguments as well as to arithmetic operands.

--
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
 
Flash Gordon
Guest
Posts: n/a
 
      12-21-2005
dragoncoder wrote:
> 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
> ?


This is a FAQ, or at least a variation on it.
http://www.eskimo.com/~scs/C-faq/q3.2.html
The comma separating arguments is *not* a sequence point, and modifying
a variable twice between sequence points is undefined behaviour.
Please check the FAQ for a group and search the group before posting
questions.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-21-2005
dragoncoder <(E-Mail Removed)> wrote:

> I think the comma (,) operator is a sequence point


It is. The comma that separates arguments passed to a function is not
the comma operator.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
dragoncoder
Guest
Posts: n/a
 
      12-21-2005
There are 2 things here.

1. Modifiying the value of a variable twice between 2 consecutive
sequence is undefined behaviour. As in

int i = 0;
int j = i++ * i++; // UB

Talking about the example in the faq, the statement printf("%d\n", i++
* i++); is a UB because the expression i++ * i++ itself is undefined.
So far so good.

2. The below code is well-defined.

int i = 0;
int j = i++;

The expression i++ is well defined and hence all the arguments of in
call printf("%d ,%d ,%d\n",i,++i,i++); are well defined. I feel this is
a UB becase the order of evaluation of function argument is undefined.
Aren't these 2 cases different. Or am I missing something. Please help
me.

Thanks in advance.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-21-2005
"dragoncoder" <(E-Mail Removed)> writes:
> This is not a homework question. I just wanted to make it short. I
> understand that modifying the value more than once between 2 sequence
> points invokes undefined behaviour as mentioned in
> http://www.eskimo.com/~scs/C-faq/q3.2.html but here scenario is
> different I believe.
>
> I think the comma (,) operator is a sequence point and therefore the
> sequence point requirement of UB is not satisfied. I feel it has to do
> something with the order of evaluation of argements.


The statement in question was:

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

There is no comma operator in that statement. The commas here are
syntactic markers that separate the function arguments.

Something like this would be well-defined:

printf("%d", (++i,i++));

And please read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) (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
 
Jordan Abel
Guest
Posts: n/a
 
      12-21-2005
On 2005-12-21, Christopher Benson-Manica <(E-Mail Removed)> wrote:
> dragoncoder <(E-Mail Removed)> wrote:
>
>> I think the comma (,) operator is a sequence point

>
> It is. The comma that separates arguments passed to a function is not
> the comma operator.


no, but it is the function-argument comma - which makes it merely an
unspecified order, not undefined behavior, IIRC
 
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