Hello!
in a case where int a=5;
if((!a)++) or (++(!a)) gives me an error message saying that the operator ++ requires l-value, but the boolian result of if (!a++) is false.
If someone would be so kind to explain this!

Recommended Answers

All 9 Replies

!a returns a boolean value which can not be incremented.

!a returns a boolean value which can not be incremented.

I still didn't get it.
What exactly happens when the compiler meets if ((!a)++).
why doesn't forward !a by 1 and the result will be true.

I still didn't get it.
What exactly happens when the compiler meets if ((!a)++).
why doesn't forward !a by 1 and the result will be true.

a++ mean a = a + 1 .
so (!a)++ mean !a = !a + 1 which !a is not a variable.

maybe you mean if( !(++a) ) , which increments the value of a then checks if the value of a is 0.

Thank you.
So please explain The compiler that accepts
if(!a++). How is this read.
I ran the code and saw it forwarded a, but I didn't understand.

maybe you mean if( !(++a) ) , which increments the value of a then checks if the value of a is 0.

How do explain that.
First a is incremented and only then does the if work. Should't the order be the oppisite.

>> Should't the order be the oppisite.
No -- the statement is evaluated from the inside out.

Here is another example of that

int bar()
{
    return 10;
}

int foo(int n)
{
    return n* 10;
}

int main()
{
   if( foo( bar() ) )
   {


   }
   return 0;
}

In the main() above, first bar() is called, then passes the return value from bar() to function foo(), after foo() is run the if part of that statement is evaluated with the return value of foo().

Always evaluate from innermost statement outwards.

Thank you dragon.
If I now understand ,then with ((!a)++) ,first !a is evaluated as the boolian result false, but then a boolian result can not be incremented.
But with (!a++), first a is incremented, and only then is ! evaluated. i.e. (!a++) ie equel to
(!(a++))

>>first !a is evaluated as the boolian result false, but then a boolian result can not be incremented.
Bingo!

>>(!a++) ie equel to (!(a++))
When in doubt, use parantheses for clarity.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.