Diabolical plus plus 
Author Message
 Diabolical plus plus

On page 137 of Ken Arnold and James Gosling's _The Java
Programming Language: Second Edition_ there is a little exercise
asking the reader the value of the following expression:

  i++ + i++ + --i  // i = 3 at start

I went through the exercise of calculating the result in my head,
then wrote the following two programs to see if the compilers
agreed with me.

Here are the programs, with a little elaboration, first in java,
then in C:

----------------------- cut here ------------------------------
class Convert {
    public static void main (String[] args) {
        int i;

        i = 3;
        System.out.print   ("First: " + (i++ + i++) + " - Second: " + i);
        System.out.println (" - Third: " + i);
        i = 3;
        System.out.print   ("First: " + (i++ + i++ + --i) + " - Second: " + i);
        System.out.println (" - Third: " + i);
    }

Quote:
}

----------------------- cut here ------------------------------
#include <stdio.h>
int main () {
    int i;
    i = 3;
    printf ("First: %d - Second: %d", (i++ + i++), i);
    printf (" - Third: %d\n", i);
    i = 3;
    printf ("First: %d - Second: %d", (i++ + i++ + --i), i);
    printf (" - Third: %d\n", i);
    return 0;
Quote:
}

----------------------- cut here ------------------------------

Each program prints two lines of numbers, of the form:

    First: X - Second: Y - Third: Z

No doubt many readers think the question of what these lines
look like is pretty easy.  But before you ask your compilers for
a definitive answer, try them in your heads and see what happens.
What will print out?  Will Java and C produce the same result?

I hereby award an A++ to anyone who gets all answers right before
testing the code, and a ++A to anyone who gets them right and can
explain them.
--



Sat, 03 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:

> I went through the exercise of calculating the result in my head,
> then wrote the following two programs to see if the compilers
> agreed with me.

> Here are the programs, with a little elaboration, first in java,
> then in C:

Since the problem was in Java, what do you care about how C treats it?
Java and C are totally different languages.

Quote:
> No doubt many readers think the question of what these lines
> look like is pretty easy.  But before you ask your compilers for
> a definitive answer, try them in your heads and see what happens.
> What will print out?

For C, the result is undefined.  The program can literally print or do
anything, including flat-out crashing.  Causing side effects to the same
object within the same sequence point results in undefined behavior.
Asking "But what will the compiler do?" is meaningless, because by the
very nature of undefined behavior, the compiler can do _anything it
pleases_ (including, regrettably, exactly what you might expect), and so
trying to analyze what it actually does is a waste of time because not
only can it vary from platform to compiler to compiler version, but also
from day to day and from machine state to machine state.

Undefined behavior is a bad.  Do not invoke undefined behavior.  Do not
try to analyze the undefined behavior that your compiler generates; it
is a waste of time and effort.

I'm not an expert in Java, so I don't know what Java has to say about
the same situation.  I would guess that due to Java's nature the result
is well-defined, but I do not know.  If indeed the result is _not_
well-defined, the book you're reading is being very disingenuous by
containing such a useless exercise.

Quote:
> Will Java and C produce the same result?

Quite literally, and without any malice:  Who cares?  What possible
relevance could that have either to a C programmer or a Java programmer?

--

 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Shooters, looters / Now I got a laptop computer
\__/ Ice Cube
    Fat Boy and Little Man / http://www.fatboyandlittleman.com/
 Watch Fat Boy and Little Man go about their antics.
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

[...Java code...]

Quote:
>         i = 3;
>         System.out.print   ("First: " + (i++ + i++) + " - Second: " + i);

A cursory inspection of _The Java Language Specification, 2nd
ed._ seems to reveal that this prints 3 + 4 = 7 and 5.

Quote:
>         i = 3;
>         System.out.print   ("First: " + (i++ + i++ + --i) + " - Second: " + i);

3 + 4 + 4 = 11 and 4.

[...C code...]

Quote:
>     i = 3;
>     printf ("First: %d - Second: %d", (i++ + i++), i);

At this point, the computer explodes.

Quote:
> No doubt many readers think the question of what these lines
> look like is pretty easy.  But before you ask your compilers for
> a definitive answer, try them in your heads and see what happens.

Why do you think that a C compiler would produce a "definitive
result"?

--
See Figure 1.
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:
>   i++ + i++ + --i  // i = 3 at start

Don't use code like this.

It's undefined.

Demons might fly out of your nose.

It's in the FAQ.
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:
>On page 137 of Ken Arnold and James Gosling's _The Java
>Programming Language: Second Edition_ there is a little exercise
>asking the reader the value of the following expression:

>  i++ + i++ + --i  // i = 3 at start

>I went through the exercise of calculating the result in my head,
>then wrote the following two programs to see if the compilers
>agreed with me.

>Here are the programs, with a little elaboration, first in java,
>then in C:

(snip Java)
>----------------------- cut here ------------------------------
>#include <stdio.h>
>int main () {
>    int i;
>    i = 3;
>    printf ("First: %d - Second: %d", (i++ + i++), i);
>    printf (" - Third: %d\n", i);
>    i = 3;
>    printf ("First: %d - Second: %d", (i++ + i++ + --i), i);
>    printf (" - Third: %d\n", i);
>    return 0;
>}
>----------------------- cut here ------------------------------

>No doubt many readers think the question of what these lines
>look like is pretty easy.  But before you ask your compilers for
>a definitive answer, try them in your heads and see what happens.
>What will print out?  Will Java and C produce the same result?

>I hereby award an A++ to anyone who gets all answers right before
>testing the code, and a ++A to anyone who gets them right and can
>explain them.

I haven't the faintest idea of what the Java program will produce,
since I don't know squat about Java. However, I'm quite sure that the
C program will print out:

First: 42 - Second: 42 - Third: 42
First: 42 - Second: 42 - Third: 42

or some other set of numbers which may be quite different.  All will
be equally correct.
Do I get at least a B?

Cam
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

[java program snipped]

Quote:
> #include <stdio.h>
> int main () {
>     int i;
>     i = 3;
>     printf ("First: %d - Second: %d", (i++ + i++), i);
>     printf (" - Third: %d\n", i);
>     i = 3;
>     printf ("First: %d - Second: %d", (i++ + i++ + --i), i);
>     printf (" - Third: %d\n", i);
>     return 0;
> }
> ----------------------- cut here ------------------------------

> Each program prints two lines of numbers, of the form:

>     First: X - Second: Y - Third: Z

> No doubt many readers think the question of what these lines
> look like is pretty easy.  But before you ask your compilers for
> a definitive answer, try them in your heads and see what happens.
> What will print out?  Will Java and C produce the same result?

> I hereby award an A++ to anyone who gets all answers right before
> testing the code, and a ++A to anyone who gets them right and can
> explain them.

The expressions may well be defined in Java, but they're not defined in C.

--
Eric Amick
Columbia, MD

--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:

> On page 137 of Ken Arnold and James Gosling's _The Java Programming
> Language: Second Edition_ there is a little exercise asking the reader the
> value of the following expression:

>   i++ + i++ + --i  // i = 3 at start

    This is undefined (in C, I don't know about Java), and could
therefore leave i with any value. You cannot modify a variable twice
without an interviening sequence point.

Quote:
> I went through the exercise of calculating the result in my head, then
> wrote the following two programs to see if the compilers agreed with me.
[snip]
> No doubt many readers think the question of what these lines look like is
> pretty easy.  But before you ask your compilers for a definitive answer,
> try them in your heads and see what happens.

Your compiler will not give a definitive answer.

Quote:
> What will print out?  Will
> Java and C produce the same result?

    Yes, and no, there's no way to tell. A conforming C compile could do
*anything* given this code.

Quote:
> I hereby award an A++ to anyone who gets all answers right before testing
> the code, and a ++A to anyone who gets them right and can explain them.

    There is no correct answer, the results of those calculations are
undefined in C.

--
Clark S. Cox, III

http://www.whereismyhead.com/clark/
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:
> On page 137 of Ken Arnold and James Gosling's _The Java
> Programming Language: Second Edition_ there is a little exercise
> asking the reader the value of the following expression:
>   i++ + i++ + --i  // i = 3 at start
> I went through the exercise of calculating the result in my head,
> then wrote the following two programs to see if the compilers
> agreed with me.

[snip the programs]

Quote:
> No doubt many readers think the question of what these lines
> look like is pretty easy.  But before you ask your compilers for
> a definitive answer, try them in your heads and see what happens.
> What will print out?  Will Java and C produce the same result?

Actually, this question is rather easy, if too tedious, to solve
in Java. In C the expression is meaningless.

I am not a Java expert, but as I understand it, Java defines the
order in which the expression is evaluated. This means that
the example expression is always evaluated from left to right, and
from this follows that it has a definite value.

My C expertise is a bit old, but last time I checked, modifying a
variable more than one time in an expression makes the whole
expression undefined. See for example comp.lang.c faq, questions 3.1
and 3.2.
(http://www.eskimo.com/~scs/C-faq/top.html)

Undefined means that anything can happen. comp.lang.c, 11.33.

Shouldn't this be required reading here? B-)

The result you get from C from evaluating this expression is
compiler dependent. I have had a long time plans to make a C compiler
which compiles this kind of expression to start NetHack, if such game
exists on the computer and do something as meaningful if it doesn't...

Quote:
> I hereby award an A++ to anyone who gets all answers right before
> testing the code, and a ++A to anyone who gets them right and can
> explain them.

Did I get these right? What did I win?

--
+++++++++[>+++++++++<-]>-.<+++++[>+++<-]++>++.<++[>++++<-]+>+.<++[>----

Quote:
>-]<-]<.>>+++++++[<++++++++++>-]++++[<+++++>-]<-.>[-]>+++[>++[<<<---->>

<>>-]<-]<<.+.>[-]++[<++>-]<.++.[-]>[-]++++[<++>-]<++.>>++[>++[>-<-]<--]
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus
Alan Meyer schrieb:

Quote:

> On page 137 of Ken Arnold and James Gosling's _The Java
> Programming Language: Second Edition_ there is a little exercise
> asking the reader the value of the following expression:

>   i++ + i++ + --i  // i = 3 at start

> I went through the exercise of calculating the result in my head,
> then wrote the following two programs to see if the compilers
> agreed with me.

> Here are the programs, with a little elaboration, first in java,
> then in C:

> ----------------------- cut here ------------------------------
> class Convert {
>     public static void main (String[] args) {
>         int i;

>         i = 3;
>         System.out.print   ("First: " + (i++ + i++) + " - Second: " + i);
>         System.out.println (" - Third: " + i);
>         i = 3;
>         System.out.print   ("First: " + (i++ + i++ + --i) + " - Second: " + i);
>         System.out.println (" - Third: " + i);
>     }
> }
> ----------------------- cut here ------------------------------
> #include <stdio.h>
> int main () {
>     int i;
>     i = 3;
>     printf ("First: %d - Second: %d", (i++ + i++), i);
>     printf (" - Third: %d\n", i);
>     i = 3;
>     printf ("First: %d - Second: %d", (i++ + i++ + --i), i);
>     printf (" - Third: %d\n", i);
>     return 0;
> }

I have no idea about Java, but in C no chance:

6.5 Expressions
2 Between the previous and next sequence point an object shall have
its stored value
modified at most once by the evaluation of an expression.
Furthermore, the prior value
shall be accessed only to determine the value to be stored.60) *

J.2 Undefined behaviour:

Between two sequence points, an object is modified more than once,
or is modified
and the prior value is accessed other than to determine the value to
be stored (6.5).

I won't try that code on my computer, it might format my harddisk ;)

Robert

 ----------------------- cut here ------------------------------

Quote:

> Each program prints two lines of numbers, of the form:

>     First: X - Second: Y - Third: Z

> No doubt many readers think the question of what these lines
> look like is pretty easy.  But before you ask your compilers for
> a definitive answer, try them in your heads and see what happens.
> What will print out?  Will Java and C produce the same result?

> I hereby award an A++ to anyone who gets all answers right before
> testing the code, and a ++A to anyone who gets them right and can
> explain them.
> --


--

I don't make my mistakes more than once. I store them carefully and
after some time I take them out again, add some new features and
_reuse_ them.
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:
>On page 137 of Ken Arnold and James Gosling's _The Java
>Programming Language: Second Edition_ there is a little exercise
>asking the reader the value of the following expression:

>  i++ + i++ + --i  // i = 3 at start

This expression is undefined in the c language and is the subject of
an oft repeated thread in comp.lang.c and even has its own section if
the c faq.

<<Remove the del for email>>
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:

> On page 137 of Ken Arnold and James Gosling's _The Java
> Programming Language: Second Edition_ there is a little exercise
> asking the reader the value of the following expression:
>   i++ + i++ + --i  // i = 3 at start

Which will show particular Java behavior, namely that the
sideeffects of multiple ++'s are well defined

Quote:
> I went through the exercise of calculating the result in my head,
> then wrote the following two programs to see if the compilers
> agreed with me.
> Here are the programs, with a little elaboration, first in java,
> then in C:

Java snipped - that code is OK.

Quote:
> #include <stdio.h>
> int main () {
>     int i;
>     i = 3;

>     printf (" - Third: %d\n", i);
>     i = 3;
>     printf ("First: %d - Second: %d", (i++ + i++ + --i), i);
>     printf (" - Third: %d\n", i);
>     return 0;
> }
> ----------------------- cut here ------------------------------
> Each program prints two lines of numbers, of the form:
>     First: X - Second: Y - Third: Z

No. The C program does not. The C program prints "You're fired."
Or "You can't program." Or it formats your harddisk. The problem is
that in C this code produces undefined behavior - you don't know what
happens, everything is allowed. Everything. I'll repeat that,
everything. No matter what happens, the result is a valid result of
running your program.

Why is this allowed? The main reason is to allow compilers to optimize.
In a statement which increments two variables, i.e. i++ + i++; the
compiler can assign the first increment task to the first CPU,
the second incerement to the second CPU, and add the values together
on the first CPU without waitng for the result of the second CPU.
Or in a single Pentium CPU, first and second integer unit. This makes
C faster as the expense of the programmer.

Furthermore, the order of evaluation of function arguments is also
up to the compiler. In printf ("First: %d - Second: %d", (i++ + i++), i)
it may pass the last argument before or after evaluation of the
i++ + i++. This differs from the java program, which passed one big
constructed argument. Again, this allows a C compiler to use
specific hardware as it was intended.

Good? Bad? Discuss that in alt.flame.senseless. But C works this way,
Java doesn't.

Quote:
> I hereby award an A++ to anyone who gets all answers right before
> testing the code, and a ++A to anyone who gets them right and can
> explain them.
> --


--
Michiel Salters


--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:

> On page 137 of Ken Arnold and James Gosling's _The Java
> Programming Language: Second Edition_ there is a little exercise
> asking the reader the value of the following expression:

>   i++ + i++ + --i  // i = 3 at start

> I went through the exercise of calculating the result in my head,
> then wrote the following two programs to see if the compilers
> agreed with me.

> Here are the programs, with a little elaboration, first in java,
> then in C:

<snip source code>

> Each program prints two lines of numbers, of the form:

>     First: X - Second: Y - Third: Z

> No doubt many readers think the question of what these lines
> look like is pretty easy.  But before you ask your compilers for
> a definitive answer, try them in your heads and see what happens.
> What will print out?  Will Java and C produce the same result?

I don't know about Java but, in C, this is terribly easy. In C, the
result is undefined. End of story. What values you happen to get on your
particular compiler are neither here nor there.

Here's why:

6.5

2   Between the previous and next sequence point an object shall have
its stored value
modified at most once by the evaluation of an expression. Furthermore,
the prior value
shall be read only to determine the value to be stored.70)

And here's footnote 70 (non-normative, but illustrative nonetheless):

70) This paragraph renders undefined statement expressions such as
    i = ++i + 1;
    a[i++] = i;
while allowing
    i =i +1;
    a[i] = i;

Quote:
> I hereby award an A++ to anyone who gets all answers right before
> testing the code, and a ++A to anyone who gets them right and can
> explain them.

I get an A++ for the C stuff, then - although I'd prefer a ++A, I think.
You can keep the Java stuff. ;-)

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:

> On page 137 of Ken Arnold and James Gosling's _The Java
> Programming Language: Second Edition_ there is a little exercise
> asking the reader the value of the following expression:

>   i++ + i++ + --i  // i = 3 at start

Well? C is not Java. I have no idea whatsoever what the value of that is
in Java, but if you had taken the effort to read the comp.lang.c FAQ,
you'd have known that in C, this produces undefined behaviour, and i may
therefore end up being "squiggle".

Richard
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus


Quote:
>On page 137 of Ken Arnold and James Gosling's _The Java
>Programming Language: Second Edition_ there is a little exercise
>asking the reader the value of the following expression:

>  i++ + i++ + --i  // i = 3 at start

>I went through the exercise of calculating the result in my head,
>then wrote the following two programs to see if the compilers
>agreed with me.

>Here are the programs, with a little elaboration, first in java,
>then in C:

>----------------------- cut here ------------------------------
>class Convert {
>    public static void main (String[] args) {
>        int i;

>        i = 3;
>        System.out.print   ("First: " + (i++ + i++) + " - Second: " + i);
>        System.out.println (" - Third: " + i);
>        i = 3;
>        System.out.print   ("First: " + (i++ + i++ + --i) + " - Second: " + i);
>        System.out.println (" - Third: " + i);
>    }
>}
>----------------------- cut here ------------------------------
>#include <stdio.h>
>int main () {
>    int i;
>    i = 3;
>    printf ("First: %d - Second: %d", (i++ + i++), i);
>    printf (" - Third: %d\n", i);
>    i = 3;
>    printf ("First: %d - Second: %d", (i++ + i++ + --i), i);
>    printf (" - Third: %d\n", i);
>    return 0;
>}
>----------------------- cut here ------------------------------

>Each program prints two lines of numbers, of the form:

>    First: X - Second: Y - Third: Z

>No doubt many readers think the question of what these lines
>look like is pretty easy.  But before you ask your compilers for
>a definitive answer, try them in your heads and see what happens.
>What will print out?  Will Java and C produce the same result?

>I hereby award an A++ to anyone who gets all answers right before
>testing the code, and a ++A to anyone who gets them right and can
>explain them.

undefined in C -- likely results are from 8 to 11 or nasal demons

Thanks. Take care, Brian Inglis         Calgary, Alberta, Canada
--

                                use address above to reply
--



Sun, 04 May 2003 03:00:00 GMT  
 Diabolical plus plus

Quote:

> On page 137 of Ken Arnold and James Gosling's _The Java
> Programming Language: Second Edition_ there is a little exercise
> asking the reader the value of the following expression:

>   i++ + i++ + --i  // i = 3 at start

> I went through the exercise of calculating the result in my head,
> then wrote the following two programs to see if the compilers
> agreed with me.

..... snip ....

Quote:
> I hereby award an A++ to anyone who gets all answers right before
> testing the code, and a ++A to anyone who gets them right and can
> explain them.

At least as far as C is concerned, it is undefined.  So consign
the book to the trash heap.

--

     http://www.qwikpages.com/backstreets/cbfalconer
     (Remove "NOSPAM." from reply address. Above works unmodified)
--



Sun, 04 May 2003 03:00:00 GMT  
 
 [ 30 post ]  Go to page: [1] [2] [3]

 Relevant Pages 

1. ** Is it a BUG in GDI plus? **

2. S-Plus

3. Show Plus/Minus in TreeView...

4. Announcing the birth of HAL 9000 (plus ObC)

5. source code required for conection c and sql*plus

6. Differences between printf and fprintf plus stdout

7. Unary plus

8. Unary plus and pointers

9. C Primer Plus excercise chap 6

10. TreeView Plus Sign Problem

11. Pros/Cons of J2EE + .NET (plus, java salaries are HOT)

12. stack plus data exceed 64K error

 

 
Powered by phpBB® Forum Software