-3

I am evaluating code & getting different outputs in C & Java. Since it's an expression, so must generate same output in both the languages. but it isn't the case on running.

Below are the code samples and more description

C:

Case 1:

    int a=101;
    printf("%d,%d", ++a,a);

Output= 102,102

Case 2:

   int a=101;
    printf("%d,%d,%d", ++a,a,a--);

Output= 101,101,101

Java:

    int a=101;
    System.out.print( ++a +"," + a + "," + a--);

Output= 102,102,102

I expect the output to be 102,102,102 for the 2nd case in C like Java. But the output is 101,101,101

  • In C, that is [undefined behavior](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points). – Elliott Frisch Jul 13 '19 at 16:32
  • 2
    "Since it's an expression, so must generate same output in both the languages" Not true –  Jul 13 '19 at 16:37
  • You should ask one question per question. And yes, your very first assumption is deeply flawed: the fact that 2 languages allow for the same syntax does not imply equal semantics. The German word pass is written like English "pass" yet they mean different things in those 2 different languages! – GhostCat Jul 13 '19 at 17:18
  • For the full story on these expressions in C, see [this canonical question and answer](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior). – Steve Summit Jul 13 '19 at 21:12

4 Answers4

0

Since it's an expression,…

That is false. As you have used ++a,a in printf("%d,%d", ++a,a);, they are two separate arguments to a function call and do not form one expression.

Since it's an expression, so must generate same output in both the languages.

That is false. C and Java have different rules for expression evaluation.

printf("%d,%d", ++a,a);

That violates a rule in C. C 2018 6.5 2 says:

If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined…

In printf("%d,%d", ++a,a);, the ++ operator has the side effect of incrementing a, and the second a is a value computation using the value of a. The rules of C do not say in which order the side effect and the value computation will occur, so they are unsequenced relative to each other. Therefore, the behavior is undefined.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

C and Java behave very differently in this respect.

You’re running into two issues with the C code:

  • C does not force a particular order of evaluation on function arguments - do not assume they are evaluated from left to right. The comma that separates function arguments is not the comma operator.

  • The ++ and operators may not apply the side effect immediately after the expression is evaluated. In an expression like x = a++ * ++b, the updates to a and b may not happen until after the assignment to x.

The C language definition explicitly calls this sort of code out as undefined behavior, meaning the compiler isn’t required to handle the situation in any particular way. Pretty much any result is possible.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-2

As far as I know in C the comma is a binary operator (just like +,-,*,/ are) which has some strange behaviour at first sight. It does evaluate the left side of the comma and discards the result of it and assign the right side instead. Maybe this will be clear with that example:

int a=1;
int b=2;
int c=(a,b); //will assign c to 2

With that behaviour in our head you need to this expression like the compiler most likely will see it. Probably something like this:

(STRING,(++a,(a,a--)))

If you exercise this statement through with the left-discard-right-assign logic the result will be a before it will be decremented. So if you have a printf printing a after this a should finally be 100.

If you want to take further look at this, this wikipedia-article should be fine.


Note: that the comma has very low preferences

Jedeiko
  • 30
  • 3
  • 1
    The comma used to separate arguments in a function is *not* the comma operator. Function arguments are not evaluated in any particular order, nor does it introduce a sequence point. – John Bode Jul 13 '19 at 17:27
-2

see , In first case :-

We are initializing a variable of type integer having value 101 ... and we are printing ++a and a ...

++a means it is a preincrement operator and a++ is post-increment operator

working :-

consider a box of a having value 101 ... ++a means preincrement operator , increments value first and update in the box and then print that value ( value into the box...)

and a++ means postincremement operator , it print the value in the box first and then increment the value in the box , and update that value in the box

same for decrement also

In case first :-

a=101 and we are printing the ++a and a ...

Now consider a box of a having value 101 ... ++a will increment the value first means it becomes 102 and update 102 value into the box of a ... now , box of a contains 102 and then there is no operation ...

so box of a contains 102 ...

so it will print 102 , 102 as a output ...

In case 2:-

a=101 and we are printing ++a , a , a--

so consider a box of a contains value 101 ....

in that case , if there is more than two operators , then bracket will be solve first

(++a,(a,a--))

first operation is a , the value in the box a will remains same ...

now , value into the box a will be 101 means remains same

next operation is a-- , it will be decrement the value into the box a to 100 ..and update the value in the box

now value in the box a will be 100

next operation is ++a , so it will be increment the value into the box of a .. first

now value into the box a will be 101 .... again

and it will print 101 , 101 , 101 as output ....

ill not starting java ... that as ill not give you ans of java ...