Exforsys

Home arrow Technical Training arrow C Tutorials

C Logical Operators

Page 2 of 6
Author: Exforsys Inc.     Published on: 30th Mar 2006    |   Last Updated on: 20th Jun 2011

C Programming - Operators

C Logical Operators

The logical operators are || for a logical or and && for a logical and and ! for logical not. The and and or operate on two boolean values of true or false. In C, an expression that evaluates to 0 is false and an expression that evaluates to non-zero is true. These operators return 1 for true or 0 for false.

Ads

The following table shows the results of applying these operators to different boolean values

Left Operation Right Result
 True   &&    True   True
 False  &&    True   False
 True   &&    False  False
 False  &&    False  False
 True   ||    True   True
 False  ||    True   True
 True   ||    False  True
 False  ||    False  False

As you can see from the table, an and operation is true only when both its operands are true. An or operation is true if either one of its operands is true.

The table also shows that these operators can do what is called “lazy evaluation” or “short circuit evaluation”. The operands are evaluated from left to right. For an and operation, if the left operand is false, the right operand is not even evaluated because the result is known to be false. For an or operation, if the left operand is true the right operand is not evaluated because the result will be true.

Here is a simple example to show the basic use of these operators:

Sample Code
  1.  #include <stdio.h>
  2.  #include <stdlib.h>
  3.  
  4.  void main()
  5.  {
  6.  char buffer[64];
  7.  int a;
  8.  
  9.  printf("Enter an integer from 1 to 10: ");
  10.  fgets( buffer, 64, stdin );
  11.  a = atoi( buffer );
  12.  
  13.  if ( a > 4 && a < 8 )
  14.  {
  15.  printf( "The number is 5, 6 or 7n" );
  16.  }
  17.  else if ( a <= 2 || a >= 9 )
  18.  {
  19.  printf( "The number is 1, 2, 9 or 10n" );
  20.  }
  21.  else
  22.  {
  23.  printf( "The number is 3, 4 or 8n" );
  24.  }
  25.  }
Copyright exforsys.com


The output from a couple of runs of this program follow:

Enter an integer from 1 to 10: 7
The number is 5, 6 or 7

Enter an integer from 1 to 10: 9
The number is 1, 2, 9 or 10

On the first run a is 7. The relational operators on line 13 then become 7 > 4 && 7 < 8 which reduces to true and true, so the whole statement evaluates to true and the printf on line 15 is run.

On the second run a is 9. The first condition on line 13 a > 4 is true, but the second condition a < 8 is false, which makes the statement false.

Execution continues on line 17 with the first condition a <= 2 being false but the second condition a >= 9 is true. Since line 17 is an or operation, the whole statement is true and the printf on line 19 is run.

It is possible to nest operations arbitrarily deep. For example you could have some code like this:

if ((( a == 3 ) || ( b >= c )) && ( d < a ))

The a == 3 would be evaluated first. If that is false then b >= c would be evaluated. If either of those comparisons are true then d < a is evaluated.

Here is an example demonstrating short circuit evaluation:

Sample Code
  1.  #include <stdio.h>
  2.  
  3.  int func( const char * op )
  4.  {
  5.  printf("Running func() for %s operationn", op);
  6.  
  7.  return 1; /* returning true */
  8.  }
  9.  
  10.  void main()
  11.  {
  12.  int input;
  13.  
  14.  printf("Enter t for true or f for false: ");
  15.  input = getchar();
  16.  
  17.  input == 't' && func("AND");
  18.  
  19.  input == 't' || func("OR");
  20.  }
Copyright exforsys.com


The output from a couple of runs of this program:

Enter t for true or f for false: t
Running func() for AND operation

Enter t for true or f for false: f
Running func() for OR operation

Lines 17 and 19 in this program are a little unusual. They perform logical operations but the results of the operations are discarded. The func() function just prints out a line and returns true.

On the first run input is 't'. On line 17 the first condition input == 't' is true so the func() function is called. This is necessary because for an and operation both left and right operands must be true for the statement to be true.

On line 19 the first condition is again true. But since line 19 is an or operation and the left operand is true the statement evaluates to true without having to call func().

On the second run input is 'f'. On line 17 the first condition is false and therefore the whole and statement evaluates to false without calling func(). On line 19 the first condition is false, but the or statement still could evaluate to true if the second condition is true, so func() is called.

Ads

The logical not operator is a unary operator, it takes one operand after the ! symbol. It inverts the logical value of the operand – a true value becomes false, and a false value becomes true. Remember that in C a non-zero value is true and zero is false.

Therefore, these lines:

int a = -97;
int b = !a;

will set b to 0 because -97 is non-zero which is true, and a logical not of true is false which is represented as 0. You can invert the logical values of other comparisons as well like this:

if ( ! ( a < 0 && b > k ))
{
/* do something... */
}

If you know boolean logic you know this is the equivalent of saying if ( a >= 0 || b <= k ).



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments