C Programming Practice 3 on Logical Operator

For Complete YouTube Video: Click Here

In this class, we will try to solve some C Programming Practice 3 on Logical Operator.

We have already solved programming practice one and programming practice two.

Programming Practice on Logical Operator

Let us consider the following two programs on logical operators.

Program 1
int a = 5, b = 6, c = 7, d = 4, e;
e = a>b && c>d || a>d;

Program 2
int a = 10, b = 10, c = 3, d;
d = a == b & c;

Program 1

Let us consider the program below.

int a = 5, b = 6, c = 7, d = 4, e;
e = a>b && c>d || a>d;

The above program has five variables a, b, c, d, and e.

The values of the variables a, b, c, d are 5, 6, 7, and 4.

The expression above has logical operators surrounded by relational terms.

To understand how the expression will be evaluated, we use the operator precedence and associativity table.

C Programming Practice 3 on Logical Operator

First, The relational expression will be executed from left to right.

The output of the expression after executing the relational terms is as shown below.

e = 0 && 1 || 1

Logical AND have the highest precedence than Logical OR operator.

From the above expression, the Logical AND will be executed first and produces an output of 0.

Now 0 || 0 will be executed and produces 1

Program 2

Let us consider the program below.

int a = 10, b = 10, c = 3, d;
d = a == b & c;

In the above expression, we have three operators Assignment operator =, Equality operator ==, and Bitwise AND operator.

From all the operators, the Equality operator has the highest precedence.

The expression after evaluating the equality operator is as shown below.

d = 1 & 3

Now the bitwise AND operator will be executed and produces one as the output.

The assignment operator has the least precedence, and d will be assigned to one.