New to C programming- Need help understand this logical expressions

This is a discussion on New to C programming- Need help understand this logical expressions within the C Programming forums, part of the General Programming Boards category; Code: int main() { int i = 1, j = 1, k = 1; printf("%d ", ++i || ++j && ...

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

    Exclamation New to C programming- Need help understand this logical expressions

    Code:
    int main()
    {
       int  i = 1, j = 1, k = 1;
      
       printf("%d ", ++i || ++j && ++k);
       printf("%d %d %d ", i, j, k);
    
       return 0;
    }
    
    For the first printf statement the output is 1.
    The second statement's output is 2 , 1, 1.
    
    I thought the results would have been:
    First statement 1.
    Second statement 2, 2, 2.
    
    Can someone please explain how this works?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Hint: the || and && operators have a "short circuit" behaviour such that if evaluating the left operand is sufficient to determine the value of the whole expression, the right operand is not evaluated.
    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

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    Is the left operand (++i || ++j)? If so, doesn't the value of j change because it is incremented before it is evaluated?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    What is the relative precedence of || and &&?

    EDIT:
    Actually, grouping does not matter so much for the value of j, though it matters for the value of k. Still, it is good to know how those sub-expressions are grouped.
    Last edited by laserlight; 07-16-2012 at 10:03 PM.
    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

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    It evaluates the left operand first, then the right operand. RIGHT?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Yes, if the left operand is not sufficient to determine the value of the expression. That is, in the case of ||, if the left operand evaluates to a false value (i.e., zero), the right operand is evaluated, otherwise the right operand is not evaluated.
    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

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    Okay, i think i am getting it. Because ++1 has a value greater than zero, there is no need for the expression to increment the value of j or k?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Yes.
    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

  9. #9
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    Okay, thank you very much. This expression was killing me, but I get it!!!!

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    O_o

    Perhaps?

    Code:
    if(true && evaluated);
    if(false && not_evaluated);
    if(true || not_evaluated);
    if(false || evaluated);
    Soma

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    Thanks Soma... this too makes sense and it's an easy reference.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    For the record even though you made a thread here, it's basically the same issue. Don't make a thread for every Boolean expression you come across.

    The NOT operator makes the result of the operand opposite. So if the result would be true, NOT true is false. NOT false is true. If you NOT a NOT expression, you have to flip the result again. You can do this many times.

    It works with a single operand, so generally the closest variable or literal is the operand. For anything else, use parenthesis.
    Last edited by whiteflags; 07-17-2012 at 12:45 AM.

  13. #13
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    1st: Okay I'll remember that... new to system. But if you post a new question to an old thread, how will it get noticed?

    2nd: I'm still not sure I get this expression: Since !i is true, then with the double negative it flips the true to false? Then it evaluates !j which is true?

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Well to start with i is 2. In C, any non-zero value evaluates to true.

    So if we evaluate again - !i is false, then NOT again makes it true. Then !j is false.

    The boolean expressions are converted to integers when they are added - so true is made 1 instead of "any number excluding zero"

  15. #15
    Registered User
    Join Date
    Jul 2012
    Posts
    10
    I get it now! I understood this !i as, since 2 is not = 0 it is true and the second NOT made it false. I was missing how C evaluates it. Thanks for the clarification


    Now if I have another question about expressions, just add to this thread and it will get noticed?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. logical not !7 logical whaaaa?
    By Charbot in forum C Programming
    Replies: 2
    Last Post: 03-22-2011, 07:19 PM
  2. Replies: 7
    Last Post: 02-15-2011, 01:50 PM
  3. Logical expressions
    By En-Motion in forum C Programming
    Replies: 7
    Last Post: 10-14-2008, 04:41 PM
  4. C gurus, how to understand these expressions?
    By thinhare in forum C Programming
    Replies: 8
    Last Post: 01-11-2005, 04:43 PM
  5. Replies: 20
    Last Post: 05-25-2002, 07:14 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