(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<metadesc>Short Circuit Rule in C language, Predict the output</metadesc>
 
<metadesc>Short Circuit Rule in C language, Predict the output</metadesc>
<syntaxhighlight lang="c">
+
===What's the output?===
#include<stdio.h>
+
<syntaxhighlight lang="c" name="shortcircuitrule">
 +
#include <stdio.h>
 
int main()
 
int main()
 
{
 
{
Line 10: Line 11:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
(A) Undefined Value  
 
(A) Undefined Value  
Line 19: Line 21:
 
(D) 2 2
 
(D) 2 2
  
===Solution===
+
==={{Template:Author|Arjun Suresh|{{arjunweb}} }}===
Anyone who have read about undefined value in C language might think that the answer here is Undefined. But, in the definition of undefined value, for expressions, its clearly mentioned about sequence points and logical operators || and && are to be considered as sequence points. That is , all the side effects of expressions should be complete before evaluation these operators. This is done so that short circuiting rule (explained below) would work in C.
+
Anyone who have read about [[Undefined Value 2|undefined value]] in C language might think that the answer here is Undefined. But, in the definition of [[Undefined Value 2|undefined value]], its clearly mentioned about [https://en.wikipedia.org/wiki/Sequence_point sequence points] and logical operators || and && are pat of sequence points. That is, all the side effects of evaluation of expression on the left side of these operators must be completed before evaluating the right side. This is done so that short circuiting rule (explained below) would work in C.
  
Short circuiting rule is used to short circuit the evaluation of an expression as soon as the result of the whole expression is known. i.e.; Once the end result of an expression is determined at some point during the evaluation of that expression, the remaining part of the expression is not evaluated. This happens in the case of logical operators && and ||.
+
Short circuiting rule is used to short circuit the evaluation of an expression as soon as the result of the whole expression is known. i.e.; Once the end result of an expression is determined at some point during the evaluation of that expression, the evaluation of the remaining part of that expression is skipped. This happens in the case of logical operators && and ||.
  
In the question here, consider the statement  
+
In the code above, consider the statement  
 
   b = ++a || ++a;
 
   b = ++a || ++a;
Here, we know the value of b, just when we see ||, as ++a returns a positive number. Hence, the compiler will skip the remaining execution of the expression and a is incremented to 2 and b is assigned the result of || operator which is 1.
+
Here, we know the end value of || operator, even before we evaluate the right hand side of ||, as ++a returns a positive number. Hence, the compiler will skip the execution of the remaining part of the expression. Thus a is incremented only once (to 2) and b is assigned the result of || operator which is 1.
 +
 
 +
Similarly for && operator, the second part of the expression wont be evaluated if the first half evaluates to 0.
  
Similarly for && operator, the second part of the expression won't be evaluated if the first half evaluates to 0.
+
Short circuiting rule is provided not only to avoid execution of dead codes but also as a means of error handling. For example, in the left hand side of && we can check
 +
the index of an array to be within the array bounds and then in the right hand side, access the array element.
  
 
{{Template:FBD}}
 
{{Template:FBD}}

Latest revision as of 14:06, 14 April 2014

What's the output?

<syntaxhighlight lang="c" name="shortcircuitrule">

  1. include <stdio.h>

int main() {

  int a = 1, b;
  b = ++a || ++a;
  printf("%d %d", a,b);
  return 0;

} </syntaxhighlight>


(A) Undefined Value

(B) 3 1

(C) 2 1

(D) 2 2

Solution by Arjun Suresh

Anyone who have read about undefined value in C language might think that the answer here is Undefined. But, in the definition of undefined value, its clearly mentioned about sequence points and logical operators || and && are pat of sequence points. That is, all the side effects of evaluation of expression on the left side of these operators must be completed before evaluating the right side. This is done so that short circuiting rule (explained below) would work in C.

Short circuiting rule is used to short circuit the evaluation of an expression as soon as the result of the whole expression is known. i.e.; Once the end result of an expression is determined at some point during the evaluation of that expression, the evaluation of the remaining part of that expression is skipped. This happens in the case of logical operators && and ||.

In the code above, consider the statement

 b = ++a || ++a;

Here, we know the end value of || operator, even before we evaluate the right hand side of ||, as ++a returns a positive number. Hence, the compiler will skip the execution of the remaining part of the expression. Thus a is incremented only once (to 2) and b is assigned the result of || operator which is 1.

Similarly for && operator, the second part of the expression wont be evaluated if the first half evaluates to 0.

Short circuiting rule is provided not only to avoid execution of dead codes but also as a means of error handling. For example, in the left hand side of && we can check the index of an array to be within the array bounds and then in the right hand side, access the array element.




blog comments powered by Disqus

<syntaxhighlight lang="c">

  1. include<stdio.h>

int main() {

  int a = 1, b;
  b = ++a || ++a;
  printf("%d %d", a,b);
  return 0;

} </syntaxhighlight>

(A) Undefined Value

(B) 3 1

(C) 2 1

(D) 2 2

Solution[edit]

Anyone who have read about undefined value in C language might think that the answer here is Undefined. But, in the definition of undefined value, for expressions, its clearly mentioned about sequence points and logical operators || and && are to be considered as sequence points. That is , all the side effects of expressions should be complete before evaluation these operators. This is done so that short circuiting rule (explained below) would work in C.

Short circuiting rule is used to short circuit the evaluation of an expression as soon as the result of the whole expression is known. i.e.; Once the end result of an expression is determined at some point during the evaluation of that expression, the remaining part of the expression is not evaluated. This happens in the case of logical operators && and ||.

In the question here, consider the statement

 b = ++a || ++a;

Here, we know the value of b, just when we see ||, as ++a returns a positive number. Hence, the compiler will skip the remaining execution of the expression and a is incremented to 2 and b is assigned the result of || operator which is 1.

Similarly for && operator, the second part of the expression won't be evaluated if the first half evaluates to 0.




blog comments powered by Disqus