-1
public class main {

public static void main(String[] args) {
  int x=20,y=35;       
  x = y++ + x++;
  y = ++y + ++x;
  System.out.printf("%d %d\n",x,y);
}
//Output : 56,93

#include<stdio.h>
void main()
{
  int x=20,y=35;       
  x = y++ + x++;
  y = ++y + ++x;
  printf("%d %d ",x,y);
}
//Output : 57 94

According to Operator Precedence Rules whatever output I got through Java code is right but when executing the same in 'C' code it incremented the output values by 1. I am using ubuntu 12.04 64-bit Operating System.

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
Software Sainath
  • 1,040
  • 2
  • 14
  • 39
  • This is undefined behavior in C and C++ – Karthik T Sep 03 '13 at 08:56
  • 2
    The C code is not valid C code. – Art Sep 03 '13 at 08:57
  • 3
    should `main` in c return int? – Philipp Sander Sep 03 '13 at 08:57
  • The behaviour in Java is defined, but it is not defined in C/C++. i.e. anything can happen in C/C++ version. – nhahtdh Sep 03 '13 at 09:00
  • @PhilippSander In C, main can be `void` or `int` and it may or may not have an arguments parameter. – Peter Lawrey Sep 03 '13 at 09:00
  • thanks! i'm not into c or c++ – Philipp Sander Sep 03 '13 at 09:00
  • 2
    @PeterLawrey “In C, main can be void or int” That's not what my copy of the C99 standard says in clause 5.1.2.2.1:1. What clause told you that `main()` could return `void`? – Pascal Cuoq Sep 03 '13 at 09:16
  • @PascalCuoq Never the less, the latest version of `gcc` accepts it. – Peter Lawrey Sep 03 '13 at 09:22
  • @PascalCuoq As this page notes, you didn't have to give any return type. http://en.wikipedia.org/wiki/C_(programming_language) The `main()` with out a return type also compiles. – Peter Lawrey Sep 03 '13 at 09:25
  • 2
    @PeterLawrey There may be difference between what compiles and what is standard/non-standard `main()` according to c standard should have a return of type `int`. – 0decimal0 Sep 03 '13 at 09:45
  • There is a big difference between what you can do and what you should do ;) – Peter Lawrey Sep 03 '13 at 09:46
  • @PeterLawrey `main()` is an old syntax for a function returning an `int`. It does not mean that the function can return `void`. – Pascal Cuoq Sep 03 '13 at 10:29
  • @PascalCuoq For me, "can" means if you do it will work for some compilers such as `gcc`. Perhaps you have a different meaning for "can" – Peter Lawrey Sep 03 '13 at 10:35
  • @PascalCuoq: from 5.1.2.2.1: "...It shall be defined with a return type of `int` and no parameters ... or with two parameters ...; or in some other implementation-defined manner.". An implementation is allowed to provide additional signatures for `main`, but it must document those signatures. – John Bode Sep 03 '13 at 11:20

1 Answers1

1

It's not the same thing because Java and C are different languages.

The behaviour of post increment in Java is defined as performing the increment after the expression has been evaluated.

The behaviour in C is undefined but often follows the order of precedence.

Note: order of evaluation and precedence are different things and this is more obvious for post increment/decrement and short cut boolean operations.

e.g.

Sting s = null;

if (s == null || !(s.length() > 0))

In this case ! has the highest precedence, but in reality will never be evaluated because the lower precedence || prevents it.

Precedence only determines where the implied brackets are, but only suggest how the expression might be evaluated.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130