-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chuck,

On 2/16/2009 5:19 PM, Caldarale, Charles R wrote:
>> From: Christopher Schultz [mailto:ch...@christopherschultz.net]
>> Subject: Re: [OT] of the different methods to get a user-id
>>
>> For instance.
>> i = i++
>>
>> yields different results depending on what language
>> you are using. C and Java produce different outputs
>> (which really surprised me!).
> 
> Java explicitly defines the behavior of the above statement, whereas
> the C standard does not, and leaves it up to the implementation to
> decide what to do. However, on almost every platform I have access to
> right at this moment (Windows, Linux, proprietary), printing the value
> of i after your test statement in both C and Java displayed the original
> value of i (as expected), not the increment thereof.

$ java -version
java version "1.5.0_13"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05)
Java HotSpot(TM) Client VM (build 1.5.0_13-b05, mixed mode)

$ javac PostIncrementTest.java (see below)
$ java PostIncrementTest
0
0
0
$ cc --version
gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.1)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cc -o post_increment_test post_increment_test.c  (see below)
$ ./post_increment_test
0 0 1
$

I'm not sure what to say. Here are the sources:

post_increment_test.c:
#include <stdio.h>

int main() {
  printf("%d %d %d\n", test1(), test2(), test3());

  return 0;
}

int test1() {
  int i = 0;

  return i++;
}

int test2() {
  int i = 0;

  return (i++);
}

int test3() {
  int i = 0;

  i = i++;

  return i;
}

PostIncrementTest.java:
public class PostIncrementTest
{
    public static void main(String[] args)
    {
        System.out.println(test1());
        System.out.println(test2());
        System.out.println(test3());
    }

    public static int test1()
    {
        int i=0;

        return i++;
    }

    public static int test2()
    {
        int i=0;

        return (i++);
    }


    public static int test3()
    {
        int i=0;

        i = i++;

        return i;
    }
}

> The one problematic environment that gave the unexpected answer was
> Visual C++ 6.0 on Windows (gee, Microsoft ignores standard practice?
> who would a-thunk it); gcc on Windows worked properly.

Do you mean gcc on win32 worked as you expected? As you said, C does not
define this behavior, so "proper" is in the eye...

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmZ6l4ACgkQ9CaO5/Lv0PB+RQCffUOOV9Lv6tuBfSoG1e5mcvmm
YA4AoJoFhjoMaIbxCwqe6nxyPfh7fhcX
=0Dpi
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to