Thread: Does C guarantee short circuit evaluation?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196

    Does C guarantee short circuit evaluation?

    As the title says..I know C++/Java do IIRC, but does C?

    ie. safe practice?
    Code:
    if (!my_struct || !my_struct->my_ptr);

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Safe... but not guaranteed to shortcircuit, far as I know...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Yes, it is guaranteed by the standard.
    Last edited by AndrewHunter; 08-19-2011 at 07:48 PM. Reason: removed nonsense
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by CommonTater View Post
    Safe... but not guaranteed to shortcircuit, far as I know...
    How would it be not guaranteed but still safe? If 'my_struct' is a pointer, and short circuiting isn't guaranteed, then there's a chance of dereferencing a NULL pointer.

    Quote Originally Posted by AndrewHunter View Post
    Yes, it is guaranteed by the standard. However note the logical OR means both sides need to be evaluated so no short circuiting is involved.
    You're saying that it's only guaranteed for logical AND? Hrm, interesting. Seeing as a logical OR doesn't require both sides to be evaluated in terms of boolean logic, I'm surprised it needs to be by the compiler.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Syndacate View Post
    You're saying that it's only guaranteed for logical AND? Hrm, interesting. Seeing as a logical OR doesn't require both sides to be evaluated in terms of boolean logic, I'm surprised it needs to be by the compiler.
    I edited out my nonsense. It is guaranteed by the standard to be short circuited and left to right evaluated for both the logical OR and AND. Sorry for my random stupidity( it happens sometimes, just ask Tater).
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by AndrewHunter View Post
    I edited out my nonsense. It is guaranteed by the standard to be short circuited and left to right evaluated for both the logical OR and AND. Sorry for my random stupidity( it happens sometimes, just ask Tater).
    Haha, happens .

    Just wanted to make sure it was reliable.

    Thanks, guys, especially for the instantaneous replies, lol.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    If you want to look it up yourself check out 6.5.14.4 for OR and 6.5.13.5 for AND in C99. It will be around there, I only have one of the draft versions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by AndrewHunter View Post
    If you want to look it up yourself check out 6.5.14.4 for OR and 6.5.13.5 for AND in C99. It will be around there, I only have one of the draft versions.
    Ha, I'll take your word for it .

    I have to get around to reading the whole draft spec when I get some free time - I'll save it until then ^_^.

    Besides, only half the battle is it being in the draft spec, the other part is the (possibly bad) assumption that what you're using is 100% compliant with the C-spec, lol.

    Also, this is C89 IIRC.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Syndacate View Post
    Ha, I'll take your word for it .
    I have to get around to reading the whole draft spec when I get some free time - I'll save it until then ^_^.
    I wouldn't rush, it isn't that interesting of a read.


    Quote Originally Posted by Syndacate View Post
    Also, this is C89 IIRC.
    The same rule applied in C89, I just quoted from C99 since it is the one I have handy at the moment.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Guaranteed short-circuiting of logical expressions has always been a feature of C. It was true when Dennis Ritchie designed and implemented the first version of C, still true in the 1989 C standard, and remains true in the C99 standard.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by AndrewHunter View Post
    I wouldn't rush, it isn't that interesting of a read.
    Ha, not expecting it to be interesting, but you're pretty much guaranteed to learn tid-bits you didn't know about the language.

    Quote Originally Posted by grumpy View Post
    Guaranteed short-circuiting of logical expressions has always been a feature of C. It was true when Dennis Ritchie designed and implemented the first version of C, still true in the 1989 C standard, and remains true in the C99 standard.
    Ah, good . Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on 16-bit system long will guarantee 4-bit integer type?
    By jackson6612 in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2011, 10:45 AM
  2. How to guarantee alignment with malloc and or new?
    By mynickmynick in forum C++ Programming
    Replies: 16
    Last Post: 08-27-2008, 02:49 AM
  3. Replies: 13
    Last Post: 12-21-2006, 05:28 PM
  4. circuit
    By theOracle in forum C++ Programming
    Replies: 13
    Last Post: 05-18-2003, 07:14 PM
  5. Contest:Collective guarantee
    By slavi in forum C Programming
    Replies: 1
    Last Post: 01-19-2003, 12:13 PM