473,480 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Order of function parameters evaluation

What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
prog.c:10: warning: operation on `i' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?

Thanks
Nov 14 '05 #1
13 2824


subnet wrote:
What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
prog.c:10: warning: operation on `i' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?


Reading the faq will be helpful.
Its located at:
http://www.eskimo.com/~scs/C-faq/top.html

Start with question 3.2 at
http://www.eskimo.com/~scs/C-faq/q3.2.html

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #2
subnet wrote:
What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}
It says the same thing as the last time someone asked this question.
;)

The order of evaluation of function arguments is unspecified.
Since the code modifies a variable more than once without a
(guaranteed) intervening sequence point, the behaviour is
undefined.
Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
prog.c:10: warning: operation on `i' may be undefined

but it prints 7 6 5 anyway.
Undefined behaviour lets an implementation do just about
anything, including executing the code and producing output
which seems valid.
Are the function arguments guaranteed to be evaluated right
-to-left or this is not specified by the standard (and the
above code produces UB)?


It is specified as being 'unspecified'. This means that an
implementation is free to choose any arbitrary ordering it
likes, including middle-to-out (whatever that might mean,)
or whatever-looks-easiest-to-compute-first.

--
Peter

Nov 14 '05 #3
su****@katamail.com (subnet) wrote:
What does the standard say about this:
somefunc(i++, i++, i++);
That it invokes undefined behaviour.
Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
Which is correct.
but it prints 7 6 5 anyway.
Undefined behaviour is allowed to do anything, including what you
thought it should do, but also, more insidiously, doing so on your
machine and crashing on your supervisor's (next-generation) computer.
Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?


The code produces undefined behaviour, but not (just) because the order
of evaluation is unspecified. It _is_ unspecified, but if that were all
you merely wouldn't know in what order it would print 5, 6 and 7.
However, that's not all. You modify i three times without an intervening
sequence point. This means that you invoke full-blown undefined
behaviour, and the program is allowed to do literally anything within
its powers, ranging from nothing to appearing to work to getting deeply
confused and printing "Wibble!". Or worse. So don't do it.

Richard
Nov 14 '05 #4
Al Bowers <xa******@rapidsys.com> wrote in message news:<39*************@individual.net>...
Reading the faq will be helpful.
[snip]
Start with question 3.2 at
http://www.eskimo.com/~scs/C-faq/q3.2.html


Yes, in fact I had already read the FAQ, and I'm aware of sequence
points. The bit I was missing (and that the FAQ say nothing about) was
that the the comma operator, *when used in a list of arguments for a
function* does NOT introduce a sequence point, while it DOES introduce
a sequence point when used in other circumstances (as written in FAQ
3.8, which does not differentiate though).
At least, this is my understanding after some google search on clc
previous posts about the argument (<cl****************@plethora.net>),
but please correct me if I'm wrong.

Thanks
Nov 14 '05 #5
subnet wrote:
Al Bowers <xa******@rapidsys.com> wrote in message news:<39*************@individual.net>...
Reading the faq will be helpful.
[snip]
Start with question 3.2 at
http://www.eskimo.com/~scs/C-faq/q3.2.html
Yes, in fact I had already read the FAQ, and I'm aware of sequence
points. The bit I was missing (and that the FAQ say nothing about)

was that the the comma operator, *when used in a list of arguments for a
function* does NOT introduce a sequence point, while it DOES introduce a sequence point when used in other circumstances (as written in FAQ
3.8, which does not differentiate though).


If you read the language grammar, you will see that a comma
used to separate function arguments is not actually an operator.

"The comma operator" refers to the comma that separates
expressions as part of a larger expression.

An operator has one or more arguments, and a result. That
cannot be said of the comma that separates function arguments.
(Same goes for the comma that separates list items in a
declaration).

Nov 14 '05 #6
subnet wrote:
Al Bowers <xa******@rapidsys.com> wrote...
Reading the faq will be helpful.
[snip]
Start with question 3.2 at
http://www.eskimo.com/~scs/C-faq/q3.2.html


Yes, in fact I had already read the FAQ, and I'm aware of sequence
points. The bit I was missing (and that the FAQ say nothing about)
was that the the comma operator, *when used in a list of arguments
for a function* does NOT introduce a sequence point,


That's because the comma token used to separate function arguments is
_not_ a comma operator, but purely a syntactic delimiter.

Consider ( and ). They have different interpretations depending on
whether they are used in function declarators, primary expressions,
or in postfix expressions (function calls.)

It's all layed down in the C grammar.

--
Peter

Nov 14 '05 #7
su****@katamail.com (subnet) writes:
Yes, in fact I had already read the FAQ, and I'm aware of sequence
points. The bit I was missing (and that the FAQ say nothing about) was
that the the comma operator, *when used in a list of arguments for a
function* does NOT introduce a sequence point, while it DOES introduce
a sequence point when used in other circumstances (as written in FAQ
3.8, which does not differentiate though).


A comma, used in the most common way in a function call, is not a
comma operator:
f(a, b, c); /* No comma operators. */

You *can* use a comma operator in a function call, but
parentheses or another kind of grouping is needed:
g(a, (b, c), d[e, f]); /* Two uses of comma operator. */

In the former case the commas are not sequence points. In the
latter case, the two instances of a comma operator are sequence
points.

Make sense?
--
"...deficient support can be a virtue.
It keeps the amateurs off."
--Bjarne Stroustrup
Nov 14 '05 #8
Try changing the calling convention to pascal.

Then running it again.
"subnet" <su****@katamail.com> wrote in message
news:9d**************************@posting.google.c om...
What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
prog.c:10: warning: operation on `i' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?

Thanks

Nov 14 '05 #9
"DHOLLINGSWORTH2" <DH*************@cox.net> writes:
"subnet" <su****@katamail.com> wrote in message
news:9d**************************@posting.google.c om...
What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
prog.c:10: warning: operation on `i' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?


Try changing the calling convention to pascal.

Then running it again.


[top-posting corrected]

The standard doesn't talk about a "calling convention" called
"pascal", and any method for specifying such a thing is non-standard.

Certainly a compiler can implement an extension that causes something
that invokes undefined behavior to behave in a specified way. It can
even define the behavior for some construct without implementing an
extension (for example, its documentation might guarantee that
function arguments are evaluated left-to-right -- encouraging users to
write non-portable code).

But the OP was specifically asking what the standard says.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 14 '05 #10
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@benpfaff.org>...
A comma, used in the most common way in a function call, is not a
comma operator:
[snip]
Make sense?


Yes (bad wording on my side also).
Thanks to everyone who replied.
Nov 14 '05 #11
*** rude top-posting fixed ***
DHOLLINGSWORTH2 wrote:
"subnet" <su****@katamail.com> wrote in message
What does the standard say about this:
.... snip ...
int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
prog.c:10: warning: operation on `i' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left
or this is not specified by the standard (and the above code
produces UB)?


Try changing the calling convention to pascal.

Then running it again.


There is no such thing in the C language. The behaviour is
undefined.

You have been asked several times to refrain from the rude
top-posting.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #12
Oh, I'm sorry I didn't mean to top post. I actually thought I would respond
to someone who has no f'n idea why i'm responding OUT of SEQUENCE.

From now on I will try to post out of sequence at your request.
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"DHOLLINGSWORTH2" <DH*************@cox.net> writes:
"subnet" <su****@katamail.com> wrote in message
news:9d**************************@posting.google.c om...
What does the standard say about this:

#include <stdio.h>
void somefunc(int a, int b, int c)
{
printf("%d %d %d\n", a, b, c);
}

int main(void) {
int i = 5;
somefunc(i++, i++, i++);
return 0;
}

Compiling with gcc -Wall it gives:

prog.c: In function `main':
prog.c:10: warning: operation on `i' may be undefined
prog.c:10: warning: operation on `i' may be undefined

but it prints 7 6 5 anyway.

Are the function arguments guaranteed to be evaluated right-to-left or
this is not specified by the standard (and the above code produces
UB)?


Try changing the calling convention to pascal.

Then running it again.


[top-posting corrected]

The standard doesn't talk about a "calling convention" called
"pascal", and any method for specifying such a thing is non-standard.

Certainly a compiler can implement an extension that causes something
that invokes undefined behavior to behave in a specified way. It can
even define the behavior for some construct without implementing an
extension (for example, its documentation might guarantee that
function arguments are evaluated left-to-right -- encouraging users to
write non-portable code).

But the OP was specifically asking what the standard says.

--
Keith Thompson (The_Other_Keith) ks***@mib.org
<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.

Nov 14 '05 #13
*** top-posting fixed ***
DHOLLINGSWORTH2 wrote:
"Keith Thompson" <ks***@mib.org> wrote in message
.... snip ...
[top-posting corrected]
.... snip ...
But the OP was specifically asking what the standard says.


Oh, I'm sorry I didn't mean to top post. I actually thought I
would respond to someone who has no f'n idea why i'm responding
OUT of SEQUENCE.

From now on I will try to post out of sequence at your request.


You make no sense, and I assume you do not understand, rather than
being simply rude. In order to keep articles readable and
understandable, most technical newsgroups require that you post
your answer after, or intermixed with, the material to which you
reply, and snip those portions that are not germane to your reply.

Here are a few things to read:

http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.greenend.org.uk/rjk/2000/06/14/quoting.html
http://www.i-hate-computers.demon.co.uk/

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #14

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

Similar topics

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...
7
2623
by: publictom | last post by:
Is the following statement true? "It is required that the operands to an operator be fully evaluated before the operator itself is evaluated." The only way I can think of that it would be...
13
2633
by: Richard | last post by:
Boy, I'll sure bet this is a FAQ. Many years ago, my "runtime behavior of programming languages" prof absolutely guaranteed that C parameters are evaluated left-to-right. He was a bright guy...
21
4084
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; }
9
2489
by: John Smith | last post by:
I've been playing with splint, which returns the following warning for the code below: statlib.c: (in function log_norm_pdf) statlib.c(1054,31): Expression has undefined behavior (left operand...
3
2788
by: andreas ames | last post by:
Hi all, recently I came across a line of code like the following: if seq.erase(seq.begin(), seq.end()) != seq.end() /* ... */ It made me wonder if this is just bogus or if it even can...
10
3287
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...
10
1566
by: somenath | last post by:
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...
15
1939
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
54
3859
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6915
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7097
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...
1
6750
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...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
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,...
0
2993
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1307
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 ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.