Logical Operators in C - C-Tutorial

Latest

Wednesday, 16 November 2016

Logical Operators in C

Logical Operators in C


  • C language supports logical operators as &&, || and ! Operators.  Logical operators are used to combine two or more relational expressions.  
  • Any expression that forms with the combination of logical operators and operands is termed as a logical expression.  Logical expressions are also known as "compound relational expressions" .

 

Logical AND, Logical OR operators are binary operators and Logical NOT is a unary operator.
Logical expressions are also produces the result values as either 1 or 0, depending on truth tables supported by the operators.

Exp1
Exp2
Exp1 && Exp2
Exp1 || Exp2
! Exp1

0
0
0
0
1
0
1
0
1
1
1
0
0
1
0
1
1
1
1
0

Here,

  • Logical AND produces result value as 1 (TRUE), if both operands are 1 (TRUE); otherwise, result value is 0 (FALSE).
  • Logical OR produces result value as 0 (FALSE), if both operands are 0 (FALSE); otherwise, result value is 1 (TRUE).
  • Logical NOT produces result value as 1 (TRUE), is the expression value is 0 (FALSE); otherwise, result value is 0 (FALSE).


/* EXAMPLE PROGRAM FOR LOGICAL OPERATORS */

#include<stdio.h>
#include<conio.h>

main()
{
clrscr();
printf("\nResult 1:%d",(14>78)&&(24<78));
printf("\nResult 2:%d",(14>78)||(24<78));
printf("\nResult 3:%d",!45);
}

No comments:

Post a Comment