473,511 Members | 17,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Evaluation of arguments in function calls

15 New Member
Link to the question

20. main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}

The output is given as 45545. But I am getting 45555. Please clarify.

In general how to resolve these kind of problems?
Jun 13 '08 #1
11 1604
jorba101
86 New Member
Link to the question

20. main()
{
int i=5;
printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
}

The output is given as 45545. But I am getting 45555. Please clarify.

In general how to resolve these kind of problems?
Nice problem.

I just realized compiling your example that parameters are read from right to left.

Anyway, I see logical the output 45545:

Take 5;

From right to left:

i : passes 5 to printf

--i: Substracts 1, and then prints. passes 4 to printf

++i: Adds 1, and then prints. passes 5 to printf

i--: passes 5 to printf, and then substracts one. We get 4

i++: passes 4 to printf, and then adds one. We get 5

Isn't it?
Jun 13 '08 #2
JosAH
11,448 Recognized Expert MVP
Isn't it?
No it isn't; according to the Standard modifying a modifiable lvalue (such as
variable 'i') more than once before a sequence point has been reached causes
undefined behaviour and anything can happen.

kind regards,

Jos
Jun 13 '08 #3
jorba101
86 New Member
Does anybody know if is it reported / documented in GCC the described behavior?
Jun 13 '08 #4
JosAH
11,448 Recognized Expert MVP
Does anybody know if is it reported / documented in GCC the described behavior?
There is no need to document that explicitly because the GCC compiler suite is
C99 conforming so that same piece of code produces undefined behaviour; just
as the Standard dictates. Of course a compiler produces 'something' but you
can't rely on it that another compiler will produce the same results.

kind regards,

Jos
Jun 13 '08 #5
jorba101
86 New Member
Of course a compiler produces 'something' but you
That's what I mean. I'm sure that GCC may produce C99-compliant code. And I'm not so sure that you can't configure it to do different things from the standard if you need too.

What I meant is that it is likely that a particular implementation like GCC produces an output which is following some kind of internal and reproduceable rule, which is also likely to be documented somewhere. Even though you can't rely another compiler will do different. But anyway, if documented, you can rely on what GCC will do.
Jun 13 '08 #6
JosAH
11,448 Recognized Expert MVP
That's what I mean. I'm sure that GCC may produce C99-compliant code. And I'm not so sure that you can't configure it to do different things from the standard if you need too.

What I meant is that it is likely that a particular implementation like GCC produces an output which is following some kind of internal and reproduceable rule, which is also likely to be documented somewhere. Even though you can't rely another compiler will do different. But anyway, if documented, you can rely on what GCC will do.
That would be just stupid; never ever rely on what one particular compiler does.
If you ever need/want to port that code to another platform you're toast.

kind regards,

Jos
Jun 13 '08 #7
Banfa
9,065 Recognized Expert Moderator Expert
In general all the problems on the page linked to produce undefined behaviour in some manor. I would recommend not trying to work through them.
Jun 13 '08 #8
jorba101
86 New Member
Hey,

That's ok. I wasn't gonna put that on production stuff.

I'm sure there's much more scaring things yet in the flashes of our TV's, DVD players or cars...

:D
Jun 13 '08 #9
jorba101
86 New Member
Anyway, following the topic on parameter evaluation order.

As per K&R2, section 2.12, parameter order evaluation is "unspecified" (in C99, in C89, or in both?, is C99 an ANSI?), so operators like "++i" should be better avoided if order is important.

Regarding modifying several lvalues at once in a statement, ok, i promise I won't do it (why couldn't just the compiler complain, to avoid these discussions?).


Btw, about GCC and C99 compliance:

http://gcc.gnu.org/gcc-4.3/c99status.html
Jun 13 '08 #10
JosAH
11,448 Recognized Expert MVP
Regarding modifying several lvalues at once in a statement, ok, i promise I won't do it (why couldn't just the compiler complain, to avoid these discussions?).
Because the compiler can't always know or see it; ex:

Expand|Select|Wrap|Line Numbers
  1. /* in file A.c */
  2. int foo(int* x) { return ++(*x); } 
  3.  
  4.  /* in file B.c */
  5. extern int foo(int* x);
  6.  
  7. int main() {
  8.  
  9.    int i= 42;
  10.    printf("i= %d\n, i+++foo(&i));
  11.    return 0;
  12. }
  13.  
kind regards,

Jos
Jun 13 '08 #11
Atos
7 New Member
I don't think that C99 is a standard in the complete sense...
Most compilers ( if not all ), do not support it.

Anyway, this shuold not be the case however, cause you are talking for a non-portable code using a standard that was made to produce portable code!

As far as i know, function arguments are not guaranteed a standard order of evaluation.
Jun 15 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

25
2364
by: Steven Bethard | last post by:
So I end up writing code like this a fair bit: map = {} for key, value in sequence: map.setdefault(key, ).append(value) This code basically constructs a one-to-many mapping -- each value that...
8
2778
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
9
2916
by: Albert Wagner | last post by:
What is the evaluation context of the setTimeout args below? I have a separate Timer instance for each sprite in my program. As coded, "this.Clock" doesn't work. Thanks ahead for any advice. ...
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...
4
2561
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....
7
1386
by: Greenhorn | last post by:
Hi, From man pages in unix it seem the behavior in below usage of printf() is undefined. But, is there something to be noted from the output of the below line. printf("%d %d", i = printf("order...
13
2828
by: subnet | last post by:
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;
25
1656
by: haroon | last post by:
Consider this function, void fun (int i, int j) { printf ("i = %d : j = %d", i, j); } I call it like this: fun (n++, n);
21
4087
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; }
3
2793
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...
0
7245
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,...
1
7085
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
7512
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
5671
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
4741
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...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
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
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
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...

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.