Microcontroller Embedded C Programming Lecture 81| Relational operators in ‘C’

  • Post author:
  • Post category:Blog

 

Relational operators in ‘C’

 

 

 

Relational operators in ‘C’

  • Relational operators do some kind of evaluation on the operands and then return value 1(for true) or 0(for false). 
  • Relational operators are binary operators because they require two operands to operate. 
  • The relational operators are evaluated left to right.

 

The available Relational operators in the ‘C’ programming language are shown in Figure 1. So use it for the evaluation.

 

Figure 1. Relational Operators
Figure 1. Relational Operators

 

Equal to (==):

First relational operator is ‘==’ (Equal to).

Syntax: expression1 == expression2

Please note the ‘==’(double equal to) sign here. Most of the beginners make mistakes by mentioning the single ‘=’ sign. So, the single ‘=’ sign is the assignment operator, not = (equal to) operator. You have to use double equal to(==) sign for equal to operator.

For example, you have two variables, a and b. And if you want to check whether a is equal to b, then you have to use the double equal to(==) operate. So, a == b returns 1 if a and b are the same. That’s very simple to understand.  

 

Greater than (>):

Syntax: expression1 > expression2

>(greater than) is another relational operator for greater than evaluation.

a>b, this expression returns 1 if a is larger than b.

 

Less than (<):

< is for Less than.

Syntax: expression1 < expression2

The less than operator checks if expression1 is less than expression2. If expression1 is less, it returns true(1); otherwise, it returns false(0).

 

Greater than or equal to (>=):

Syntax: expression1 >= expression2

The greater than or equal to operator checks if expression1 is greater than or equal to expression2. If expression1 is greater or equal, it returns true; otherwise, it returns false.

a>=b returns 1, if a is larger than or equal to b

 

Less than or equal to (<=):

Syntax: expression1 <= expression2

The less than or equal to operator checks if expression1 is less than or equal to expression2. If expression1 is less or equal, it returns true; otherwise, it returns false.

 

Not equal to (!=):

Syntax: expression1 != expression2

Suppose if you do a!= b, this expression returns 1 if a is not equal to b. So, if a == b, then this expression will give you 0. That’s why relational operators are evaluation operators. So, they will either turn into 1 or 0. 

 

‘True’ and ‘False’ in C

  • In ‘C,’ Zero is interpreted as false and anything non-zero is interpreted as true. 

In ‘C,’ 0 is false, and anything non-zero is true. So, -1 is true, -9 is true, so non-zero means true in ‘C’. 

  • The expressions using relational operators evaluate to a value of either TRUE(1) or FALSE(0)
  • Relational expressions are often used within if and while statements. 

 

 

Examples:

Let’s see some examples.

Figure 2. Examples
Figure 2. Examples

 

You have two variables, A and B. A=10 and B=20. 

Let’s take a look at the C= (A==B) statement. This is a valid ‘C’ statement. Remember that ‘=‘ is the assignment operator, and ‘==‘ is equal to operator. Here, look into this expression A == B, which means you are checking whether A is really equal to B or not. If A and B are the same value, then this expression evaluates to be TRUE(1); otherwise, FALSE(0). Since A is not equal to B in reality, the A==B expression is evaluated to be FALSE(0). So, C = 0 in this case. 0 will be stored in C. 

Here, take C=(A<=B) this expression. This expression will yield you 1 because A is less than B. So, C = 1 in this case. 

 

Figure 3. Example
Figure 3. Example

 

Let’s look into this C=(A!= B) expression,  which is true. A is not equal to B. So, C will be 1 in this case.

Take C= (A<B).In this expression, A is less than B. So, C=1.

 

 

Get the Full Course on Microcontroller Embedded C Programming Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply