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?
11 1604 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?
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
Does anybody know if is it reported / documented in GCC the described behavior?
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
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.
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
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.
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
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
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: -
/* in file A.c */
-
int foo(int* x) { return ++(*x); }
-
-
/* in file B.c */
-
extern int foo(int* x);
-
-
int main() {
-
-
int i= 42;
-
printf("i= %d\n, i+++foo(&i));
-
return 0;
-
}
-
kind regards,
Jos
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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.
...
|
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...
|
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....
| |
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...
|
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;
|
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);
|
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;
}
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |