Skip to content
Advertisement

How the output of the below code becomes 1 need good explanation

The result of the above program is 1 but I am not sure how its giving the result of 1. someone pls explain this to me.. it was asked in one of the interview I had for automation

    a = 1111;
    a = a-- -a;

Advertisement

Answer

So at first break this statement and try to understand what’s going on

a = a-- -a;

So you must be aware with operator precedence

so here what’s going on is

at first a– is getting evaluated as it is postfix decrement

Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased).

so the expression is becoming like this

a = 1111 - 1110
so a = 1
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement