19

I have heard of the term "short-circuiting" being used in C, C++, C#, Java, and many others. What does this mean and in what scenario would it be used?

3
  • 7
    There is a Wikipedia Article about the concept: en.wikipedia.org/wiki/Short-circuit_evaluation It is an optimization in the evaluation of the && operator.
    – wirrbel
    Jun 18, 2013 at 13:56
  • 2
    @wirrbel I believe it applies to || as well... at least it should. Jun 18, 2013 at 18:36
  • 1
    @RaduMurzea Indeed. Contrast || and && to & and | to see the subtle difference. Have a simple program evaluate 1 || printf("yay"); vs 0 || printf("yay"); and 1 | printf("yay"); vs 0 | printf("yay"); to see the differeces
    – wirrbel
    Jun 18, 2013 at 20:25

3 Answers 3

42

Short circuiting in C is when a logical operator doesn't evaluate all its arguments.

Take for example and &&, it's pretty obvious that 0 && WhoCares is going to be false no matter what WhoCares is. Because of this, C just skips evaluating WhoCares. Same goes for 1 || WhoCares, it'll always be true. Because of this, we can write code like

CanFireMissiles && FireMissiles()

This way we avoid doing some potentially impossible operation. If we can't fire the missiles we certainly don't want to try to. This is commonly used with pointers, especially file pointers.

 bool isN(int* ptr, int n){
     return ptr && *ptr == n;
 }

This plays out in lots of other useful ways to avoid unnecessary computing

 isFileReady() || getFileReady()

This avoids doing extra work if we don't need to.

4
  • 2
    Anytime, if I've answered your question you can check the checkbox beside it to mark your question as answered Jun 18, 2013 at 14:16
  • 8
    I don't love CanFireMissiles && FireMissiles(), as it makes me suspect you're abusing short-circuiting to trigger side effects. I feel like you're hiding actions in a conditional. Such code is better written as if(CanFireMissiles){FireMissiles();} or if(CanFireMissles){didFireMissiles = TryFireMissiles(); if(didFireMissiles){...}}.
    – Brian
    Jun 18, 2013 at 20:51
  • 2
    I'd argue that the only use is to hide side effects. Usually not the the "Blowing up a city" sort but things like dereferencing a pointer or using system resources are also done in this manner in C quite often. See the wikipedia page, the whole section under usage is "Hiding side effects" Jun 18, 2013 at 20:58
  • 3
    @jozefg you can also use it to prevent doing expensive operations like IsInCache(value) || IsInDatabase(value), where IsInDatabase might take time (especially if using a mobile device and network latency is an issue).
    – mgw854
    Nov 12, 2015 at 18:00
4

"Short Circuiting" typically refers to "Short Circuit Evaluation" which is a general concept, not just C specific.

Boolean operators evaluation left to right, so any terms that will render the other terms unnecessary are useful. So you might check for a condition that excludes other conditions later on, thus allowing a partial evaluation of the logical operations rather than evaluating the whole thing.

Example:

while((x && y) == 1) {
    //This bit will not execute if x is 0 or y is 0 but y won't even be 
    //evaluated due to short circuit evaluation if x is 0.
}

A more complex example:

if((a || b || c || d || e || f || g || h || i || j || k) == 1) {
    /* If any of these are equal to 1 the whole expression is equal to 1,
     * thus doesn't it make sense to short circuit evaluate this?
     * Saves a bunch of time.
     */
}
2
  • 8
    Short circuiting is less about saving time but more about not being evaluated. A function not being evaluated will also not have the side effect would it be evaluated.
    – Pieter B
    Jun 18, 2013 at 14:14
  • You know, the == 0 is not only unneccessary, it might actually confuse some people. Nov 13, 2015 at 3:55
3

Short ciruit evaluation can lead to some parts of a condition not be evaluated.

For example:

if (true || f()) { ... }

will not exectue f.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.