explain the output:--

int main(void) {
int a=10,b;

I am not understanding it. the precedence of bracket() is greater so b is assigned to a so expression becomes-
a=a+a-a which should be equal to a but it is not why?

can you explain me why..??

I did explain why: the expression is undefined, completely unpredictable, and there's no sense in trying to analyze whatever garbage results you get.


that the thing i am asking why it is unpredictable..there should be some reason .
whats happening actually with the expression after compilation , what machine is understanding to the expression, I just want to know that..

that the thing i am asking why it is unpredictable..there should be some reason.

I didn't write the language specification, and I wasn't privy to the discussions that produced this particular rule. I suspect that even if you find someone who was part of the standards committee at the time and remembers the discussion, you wouldn't understand his explanation.

So, how about not obsessing over something that's flat out wrong anyway, and focus on learning to do things the right way. I can show you the rule that's in play here and I can show you how to recognize violations of that rule, but I'm sick and tired of seeing people waste their time trying to explain undefined behavior.

learning comes from the mistakes , if i could know how its storing garbage value n how its working .... wat machine is thinking of the code.
if i would be able to understand the code..i can simplify any expression.

this is also a part of learning its not wastage of time, one should know why this behavior is occuring and I just wanted to know this and I hope daniweb should help me in this regard.

learning comes from the mistakes

Hopefully one day you'll learn that trying to define undefined behavior is a mistake. Good day.

okay...atleast explain me why its an undefined behavior...
only just printing garbage value make the expression undefined, or its just not following the standard so its undefined.
please I want to know why it is undefined.

Here's a quote from the C standard:

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.71)

Footnote 71 is as follows:

This paragraph renders undefined statement expressions such as

i = ++i + 1;
a[i++] = i;

while allowing

i = i + 1;
a = i;

What I am analyzing(as Narue told not to do that,but due to curiosity i did) is that,

for the parenthesis part a's value is assigned to b, but b outside a is not assigned i.e., it's not containing the value that is done in parenthesis.
so it's (b) containing the garbage value and final calculation is printing garbage number
if suppose the code is:--

int main(void) {
int a=10,b=20;

the code is interpreted as a=10+20-(10)=20
i.e, 'b' inside the parenthesis is assigned the value of a but not the 'b' which is outside the parenthesis, i.e, what is the value of 'b' is assigned is the output as the expression is evaluating as:--

for any value of a and b within the range of int it will give the output..

I hope you have understood the point..:)

This question has already been answered. Start a new discussion instead.