Thread: New to C Programming- Need help understanding !! operator

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    10

    New to C Programming- Need help understanding !! operator

    Code:
    int main(void)
    {
       int i =2, j = 1;
    
       printf("%d",  !!i + !j);
    
    
       return 0;
    }
    
    Working on exercises from my c book.
    
    I know the output is 1... not sure why. I read this would be called a cast type for bool.  Is the !i = 1 and the !j = 1 and since 1 = true for bool, then the value is 1? 
    
    How does the double negative work?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    ! is logical negation operator in C. In C anything of a natural number is considered to be TRUE. In discrete maths a set of natual number that is "for all elements in set (natural numbers) shall be true"

    So for example

    int i = 1;

    'i' now contains natural numbera; which is term of predicate hold to be TURE. When you negate it you get FALSE

    TRUE <==> 1
    FALSE <==> 0

    i=1 <==> TRUE
    !i <==> FALSE (!TRUE)
    !!i <==> TRUE ( ! ( ! TRUE ) )

    Therefore
    !!i <==> TRUE <==> 1
    !j <==> FALSE <==> 0

    0 + 1 <==> 1

    Thus the result is 1.

    ssharish

    PS: Set of natual numbers 0, 1, 2, .... n
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ssharish2005
    In C anything of a natural number is considered to be TRUE.
    PS: Set of natual numbers 0, 1, 2, .... n
    Rather, in C, zero is considered a false value and non-zero is considered a true value. It is not about "natural numbers", unless you use the definition of the set of natural numbers that excludes 0, but even then you would have said nothing about negative numbers and non-integers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Very ture! I quite missed that. I was thinking set of integers and Natural numbers when i was typing. Explicit description like yours perhaps will make it clear.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    I get it now. Thanks for showing the steps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding the modulus operator
    By matrixx333 in forum C Programming
    Replies: 5
    Last Post: 10-01-2009, 06:06 AM
  2. Not understanding CPU C programming benchmarks...
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-22-2005, 03:49 AM
  3. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 12:24 PM
  4. Understanding Programming Industry
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 07:26 PM

Tags for this Thread