Remember the difference C

= = is used for comparison of two quantities and = is used for assignment These operators may be used to form logical expressions. The operands may themselves be other types of expressions. The numeric value of logical expression is 1 if the relation is true and 0 if the relation is false.

Expression using relational and equality operators are shown below

count <= 100
sqrt (a + b + c) > 0.0005
answer = = 0
letter ! = ‘x’
ch <’T’

All relational operators have same precedence followed by equality operators and after that are logical operators. Relational operators have lower precedence than arithmetic operators.

Value of count < = 100 is 1, if count has value which is less than or equal to 100.

If count has a value greater than 100, this expression evaluates to false.

Value of ch <’T’ is 1, if ch holds a character whose value is less than ‘T’ and it evaluates to false if ch has a value greater than ‘T’.

Expressions connected with logical operators are evaluated from left to right and evaluation stops astruth or falsehood is known.

The truth tables for logical operators can be given as follows:

truth tables for logical operators

AND Operator equals true if both its operands evaluates to true.

AND Operator equals true

OR operator evaluates to true if either of its operand evaluates to true, else it equals false.

Negation operator

The unary negation operator (!) converts a non-zero operand into 1.

(! valid)
Instead of (valid = = 0), where valid is any type of variable.
The following expression using logical operators
( (count <= 100) && (ch ! = *) )
is 1, if count is less than or equal to 100 and at the same time ch does not contain a ‘*’

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

C Topics