Short circuit evaluation

This is a discussion on Short circuit evaluation within the C Programming forums, part of the General Programming Boards category; I'm currently studying C and I'm having a hard time wrapping my head around how exactly short circuit evaluation works. ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    British Columbia
    Posts
    3

    Short circuit evaluation

    I'm currently studying C and I'm having a hard time wrapping my head around how exactly short circuit evaluation works.
    Two examples provided to me were:
    Code:
    int a = 1, b = 1, c = -1;
    c = --a && b--;
    printf("%d %d %d", a, b, c);
    
    a = 0, b = 0, c = -1;
    c = a++ || ++b;
    printf("%d %d %d", a, b, c);
    I know from my notes that the first evaluation returns 0 1 0
    and the second evaluation returns 1 1 1. What I can't quite seem to get is why?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    How are you on figuring out say
    --a ; c = a && b;

    The only tricky part is working out whether the RHS gets evaluated at all. If it doesn't, then the side effect doesn't happen either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Location
    British Columbia
    Posts
    3
    Quote Originally Posted by Salem View Post
    How are you on figuring out say
    --a ; c = a && b;

    The only tricky part is working out whether the RHS gets evaluated at all. If it doesn't, then the side effect doesn't happen either.
    So I think in that case 'a' would = 0, therefore c = 0 because the statement is false? I'm mostly confused by c = -1. So what I've been thinking is that -1 = 0 && 1
    or
    -1 = 0 || 1

    I'm starting to think that the -1 has nothing to do with the evaluation.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    It doesn't. Assignment will overwrite the previous value of the left hand side.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Location
    British Columbia
    Posts
    3
    Now it makes perfect sense. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-01-2012, 10:57 PM
  2. How come short + int = int but short + uint = long?
    By y99q in forum C# Programming
    Replies: 2
    Last Post: 10-29-2011, 11:16 AM
  3. Does C guarantee short circuit evaluation?
    By Syndacate in forum C Programming
    Replies: 10
    Last Post: 08-19-2011, 08:31 PM
  4. eulers circuit
    By zarganah in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2004, 05:25 PM
  5. circuit
    By theOracle in forum C++ Programming
    Replies: 13
    Last Post: 05-18-2003, 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