undefined in c
hello,
seemingly the following code: Code:
here i should mention that these are some undefined in C(K&R didn't say anything if such a situation arises). nearly all the compilers(atleast for those which i have tested) produces an output of '31'. can you please say how the compilers handle such a situation. in java however you get the correct answer, i.e., 30. why do the results differ when they are actually the same. |
I believe that C and its compilers don't want you to do multiple operations on the one item (the variable i) at the same time. If you add a third variable, everything is fine:
Code:
telemachus ~ $ cat count.c |
Quote:
|
When i++ is used, it is said that variable i is increased 'after evaluation'. If it were increased before evaluation, the outcome would be 32 as both i and j are increased.
The difference that i seems to be increased before evaluation is obviously caused by using i as the result as well. Which is also demonstrated by the example of Telemachos. It would be interesting to see the assembly code produced by the compiler, could someone do that? (I know it is possible, but I forgot long ago how to do that) I am also wondering if there is some specification stating that this kind of evaluation is correct/not correct/undefined when the result is one of the operands. Last, unless your goal is to write obfuscated code, it is best to avoid such statements. However from a scientific point of view it is certainly interesting to look into it. jlinkels |
Quote:
1) temp = i + j 2) i++ 3) j++ 4) i = temp But the sequence of those operations is not fully defined. We know 1 comes before any of 2, 3 or 4, but we don't know whether 2 comes before or after 3 or 4. It is perfectly legitimate for the compiler to choose the sequence: temp = i + j i = temp j++ i++ So 30 is not "the correct answer". It is one possible answer. 31 is an equally valid answer. |
Quote:
|
please see
http://www.parashift.com/c++-faq-lit...html#faq-39.15 on sequence points or the corresponding section of the C faq's (the discussion applies to both languages). |
I hate it when people want a reasonable explanation for completely unreasonable cases like this example.
To add insult to injury, Java does *not* "give the correct answer. The snippet as posted won't even *compile* in Java (and it may or may not compile in C - "it depends"). Why would anyone want to waste any time on silly, nonsensical, pointless questions like this? If there's a pressing *need* to experiment in the land of "undefined behavior" - why not experiment on your own car (or any other handy motor vehicle)? Drain all the oil. Drive it around as long as you can. Does the engine seize up? Or does it just explode in a ball of flame? Why one and not the other? Sigh... |
Quote:
When children do pointless things in play, it's easy to smile at them. But this is their work. This is the way they join battle with the universe. Their work is just as serious as our work. When we play with undefined behavior in a C program, we are reappropriating for ourselves the wisdom we once had as children. An aside: Toy C programs are cheap. Much cheaper than automobile engines. |
Quote:
|
This must be unique to integral types, i.e. some sort of built-in operator ambiguity. Take the following example, compiled with g++:
Code:
#include <stdio.h> Code:
#include <stdio.h> Kevin Barry edit: If that weren't enough, try this one. I get 30! Code:
#include <stdio.h> |
Quote:
Kevin Barry |
Quote:
Code:
i= i++ + j++; Quote:
Quote:
Quote:
Code:
#include <stdio.h> 30 31 |
Quote:
Quote:
Personally, I spend some of my time playing around with totally useless things, weird C constructs was one of them when I was still regularly programming in C. And ...uhm if you look on the internet for totally useless scientific or technical experiments, I bet you'll find quite a few hits. jlinkels |
Quote:
Quote:
Kevin Barry |
All times are GMT -5. The time now is 06:35 PM. |