New to C Programming- Need help understanding !! operator

This is a discussion on New to C Programming- Need help understanding !! operator within the C Programming forums, part of the General Programming Boards category; Code: int main(void) { int i =2, j = 1; printf("%d", !!i + !j); return 0; } Working on exercises ...

  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,682
    ! 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
    22,274
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,682
    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, 07: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, 04:49 AM
  3. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 01: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


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