the code is:

i = 2; j = 1

printf("%d", !!i + !j);

(the answer comes out to be 1... i don't understand why [and i don't know what the "!!" means (only the "!")...

Printable View

- 07-22-2002JohnMayerQuestion about logical operators
the code is:

i = 2; j = 1

printf("%d", !!i + !j);

(the answer comes out to be 1... i don't understand why [and i don't know what the "!!" means (only the "!")... - 07-22-2002gabulldog
What your code is saying is not(not)i + not j, which means i is a true statement, regardless of the integer you assigned and j is a false statement. So, in effect, your code is saying true + false. That will equal 1. If you change it to !i + !j, it will equal 0.

- 07-22-2002quzah
!! means not not.

x = 1;

if( x ) //true

if( !x ) //false

if( !!x ) //true

Recall that any non-zero value equates to true in a true/false test. Thus,**not one**means zero (or false), so**not zero**means true.

Quzah. - 07-22-2002CAP
Hmmm...two negatives equal a positive thing right...algebra strikes back. Then again it could mean other things but I dont' know how big Simpsons fans you people are so nevermind ;)