Question about logical operators

This is a discussion on Question about logical operators within the C Programming forums, part of the General Programming Boards category; the code is: i = 2; j = 1 printf("%d", !!i + !j); (the answer comes out to be 1... ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    Question 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 "!")...

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    45
    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.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    !! 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.
    Hope is the first step on the road to disappointment.

  4. #4
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    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
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. Newbie with C++ NOT, OR, AND operators question...
    By JiWiz in forum C++ Programming
    Replies: 4
    Last Post: 07-27-2008, 12:05 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question about operators.
    By Liger86 in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21