Java vs C output

It may seem simple, but it just came across me and my friends ...

allows you to take the following code fragment: in java

//........ int a=10; a= a-- + a--; System.out.print("a="+a); //........ 

in c

 //........ int a=10; a= a-- + a--; printf("a= %d",a); //....... 

where in the first case you get the output as 19 in C, you get it as 18. The logic in c is understandable, but in java?

in java if it looks like

 int a=10; a=a++; 

in this case, the output is 10.

So what is the logic?

+4
source share
6 answers

a = a-- + a-- causes undefined behavior in C. C does not determine which decrement should be evaluated first.

a-- evaluates the value of a and then decreases a, so in Java a = a-- + a-- the following is a = a-- + a-- :

a = (10, decrement a) + (9, decrement a)

The second operand is 9, because the first member forced to decrement.

In conclusion: With this expression, C does not define the evaluation order. Java defines this from left to right.

+13
source

I do not know about Java, but in C this line of code does not have a return value defined in the standard. Compilers are free to interpret this as they please.

+4
source

In expression

 a = a-- + a--; 

you have many subexpressions that need to be evaluated before the entire expression is evaluated.

 a = a-- + a--; ^^^ <= sub-expression 2 ^^^ <= sub-expression 1 

What is the value of subexpression 1? This is the current value of object a .
What is the value of a ?

If subexpression 2 has already been evaluated, the value of the object a is 9, otherwise it is 10.

The same goes for subexpression 2. Its value can be 9 or 10, depending on whether subexpression 1 has already been evaluated.

The C compiler (I don't know about Java) is free to evaluate subexpressions in any order

So, let's say the compiler decided to leave -- for the last

 a = 10 + 10; a--; /* 19 */ a--; /* 18 */ 

but in the next compilation the compiler did -- in front

 /* value of sub-expression 1 is 10 */ /* value of sub-expression 2 is 9 */ a = 10 + 9; /* a = 9 + 10; */ 

or he can even save one of a-- and use this for the final value of a

 /* sub-expression 1 yields 10 and sets the value of `a` to 9 */ /* so yield that 10, but save the value for the end */ a = 10 + ???; a = 18???; a = 19???; /* remember the saved value */ a = 9 

Or, when you invoked undefined behavior, it could just replace your statement with any of the following

  • a = 42;
  • / * * /
  • fprintf (stderr, "BANG!");
  • system ("format C:");
  • for (p = 0; p <MEMORY_SIZE; p ++) * p = 0;
  • etc.
+2
source

You are post-decrementing. For me, the java result makes sense.

The first a-- evaluates to 10 and decreases to 9. 10 is the value of this subexpression.

Second grade a--. and now 9 and decreases to 8. The value of this subexpression is 9.

So, it becomes 10 + 9 = 19. Two decrements are overwritten by the destination.

I would expect 18 if the expression were a = -a + --a.

Have you tried compiling version C with different optimization flags?

0
source
 a = 10 + 9 

you can try:

 a = a-- + a-- + a-- 

it returns 27 (10 + 9 + 8) ...

0
source
 a= a-- + a--; 

This causes undefined behavior in C / C ++. You should not expect consistent results from this statement.

0
source

All Articles