Technical Training
C TutorialsTable of Contents
C Programming - Operators
C Logical Operators
C Assignment Operators
C Conditional Operator
C Bitwise Operator
C Special OperatorsC Logical Operators
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.
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:
- #include <stdio.h>
- #include <stdlib.h>
- void main()
- {
- char buffer[64];
- int a;
- printf("Enter an integer from 1 to 10: ");
- fgets( buffer, 64, stdin );
- a = atoi( buffer );
- if ( a > 4 && a < 8 )
- {
- printf( "The number is 5, 6 or 7n" );
- }
- else if ( a <= 2 || a >= 9 )
- {
- printf( "The number is 1, 2, 9 or 10n" );
- }
- else
- {
- printf( "The number is 3, 4 or 8n" );
- }
- }
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:
- #include <stdio.h>
- int func( const char * op )
- {
- printf("Running func() for %s operationn", op);
- return 1; /* returning true */
- }
- void main()
- {
- int input;
- printf("Enter t for true or f for false: ");
- input = getchar();
- input == 't' && func("AND");
- input == 't' || func("OR");
- }
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.
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 ).
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







