9.14.2012

Short-Circuit operators

C language has two types short-circuit operators:

  1. Logical AND operator  &&
  2. Logical OR operator  ||
What is meaning of short-circuit


In C language, the meaning of short-circuit is that if there are any expression, then output of expression must be determined by the left side operand, and right side operand never be executed.
Here two case occurred,:
what would be output if left side operand is true?
what would be output if left side operand is false?

keep in mind: 0 or any negative value is always false.
                    1 or any non-zero value is always true.


Let's understand short-circuit operators by example:

x && y

hear, if left side operand x is false then overall expression to be false, and the right side operand y never evaluated.
If left side operand x is true then result of expression depend upon  right side  operand y and in this case  right side  operand will be evaluated.

m || n

here, if left side operand m is true then overall expression to be true, and right side operand n not to be evaluated.

Read following programs carefully:

/*c program for logical OR short-circuit operator example*/
#include<stdio.h>
int main()
{
 int x, y , z;
 x = y = z = 1;

 ++x || ++y;

 printf("\n x=%d\t y=%d\t z=%d \n", x,y,z );
 getch();
 return 0;
}

The output of above program would be:

x=2  y=1  z=1
explanations: In above program logical OR short-circuit operator, in expression left hand side operand x is true so its increase ++x i.e. x=2. And right side operand y not to be evaluated.

-------------------------------------------------------------------

/*c program for exercise of logical OR short-circuit operator operators*/


#include<stdio.h>
int main()
{
 int x, y , z;

 x = y = z = 0;

 ++x || ++y;

 printf("\n x=%d\t y=%d\t z=%d \n", x,y,z );
 getch();
 return 0;
}

The output of above program would be:

x=1  y=0  z=0
explanations: In above program logical OR short-circuit operator, in expression left hand side operand x is false so right side operand y not to be evaluated.


-------------------------------------------------------------------

/*c program for exercise of logical AND short-circuit operators example*/


#include<stdio.h>
int main()
{
 int x, y , z;

 x = y = z = 1;

 ++x && ++y;

 printf("\n x=%d\t y=%d\t z=%d \n",x,y,z );
 getch();
 return 0;
}

The output of above program would be:

x=2  y=2  z=1
explanations: In above program logical AND short-circuit operator, in expression left hand side operand x is true(++x) so right side operand y also evaluated(++y).


-------------------------------------------------------------------

/*c program for logical AND, logical OR short-circuit operators example*/

#include<stdio.h>
int main()
{
 int x, y , z, r;


 x = y = z = 1;
 r = ++x || ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x && ++y || ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x || ++y || ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x && ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = 1;
 r = ++x || ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);



 x = y = z = -1;
 r = ++x || ++y && ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x && ++y  ||  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x  ||  ++y  ||  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x  &&  ++y  &&  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);


 x = y = z = -1;
 r = ++x || ++y  &&  ++z;
 printf("\nr=%d", r);
 printf("\nx=%d y=%d z=%d\n", x,y,z);

 getch();
 return 0;
}

The output of above program would be:


Output of short circuit operators C program
Screen shot for logical AND, logical OR
short circuit operator C program


1 comment:

  1. Thanks for your clear explanation but
    x = y = z = -1;
    r = ++x && ++y || ++z;
    printf("\nr=%d", r);
    printf("\nx=%d y=%d z=%d\n", x,y,z);

    You have mentioned the answer as r=0,x=0,y=-1,z=0.
    Why does the z value is changed from -1 to 0?
    please do reply

    ReplyDelete