What's the meaning of !int_variable--?

80 views Asked by At

I can't understand what the following code is doing on s:

 if(!s--)

s is an int

2

There are 2 answers

0
Natasha Dutta On BEST ANSWER

! is called negation operator. It is a logical operator.

See the wikipedia entry here.

if(!s--)

The order in which it executes

  1. check the value of s is 0 or not , if s is 0, if condition is success [thanks to the ! operator], otherwise, failure.
  2. After that, decrement s by one unit.
  3. Based on the evaluation of if condition, continue the execution [code under if condition, or next block of code].
0
blue112 On

Actually, it's misleading.

You are testing is s is different from 0 (with if (!s)). And then, afterward, whatever the result is, you're decreasing it.

So, it's two different operations. It could be written this way :

if (!s)
{
     s--;
     //...
}
else
{
     s--;
}