Relational Expressions in C Programming: Types & Examples

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adunct professor of computer science and computer programming.

Programming is full of comparisons: Is the current high score more than the previous score? Is 7 less than x? In this lesson we will define relational expressions in C, providing working code examples.

Relational Expressions

As you begin programming, you will find that relational expressions are everywhere. Is X greater than Y? Is the switch on? A relational expression is a test between two or more statements. If the test passes, then the result is TRUE. Otherwise it is FALSE.

You will also hear the phrase evaluate to True, or Evaluate to False. If the expression is true (e.g., 7 IS less than 8), then we say the expression evaluates to True.

For example, consider the following relational expression:

  • 3 is greater than 7

The result of this relational expression is FALSE.

Expressions in C

In C, as in most programming languages, there are special characters for relational expressions. You will recognize many of them from standard mathematical tests. For example, the previous statement can be written as 3 > 7 (which is still false!). Below are the operators used in C. We will cover each one in detail, including the very useful not operator.

Expression Function
== Equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
!= Not equal to
! NOT operator

Equal To (==)

If you want to see if two or more values are equal, use the == operator. For example:

if(x==y) { }

The above code checks to see if x is equal to y. Why isn't it x=y?

IMPORTANT! In C, and in most languages, if you use a single equal sign to check for equal values, you will actually ASSIGN one variable's value to another! The single equal sign is assignment, meaning you are setting the value. Compilers don't always catch this, so be sure to proof your code!

The following code will always display PASS for the output:

int main(void) {
  int x = 7;
  int y = 10;
  if(y = x) {
   printf("%s", "PASS");

Change the = to == and the if test will not display pass (the expression will evaluate to False) because 7 doesn't equal 10.

if(y == x) {

Less Than (<)

When checking if a value is less than another value, use the less than (<) operator. In the example below, we are assuming that we still have the same integer values as above (x = 7; y = 10).

The following code will evaluate to TRUE and the statements in the if block will be evaluated.

if(x < y) {
  printf("%s", "PASS");

On the other hand, this code will evaluate to FALSE:

if(y < x) {
  printf("%s", "PASS");

Less Than or Equal (<=)

The less than or equal to (<=) operator is the same as the less than, except it also checks to see if the values might also be equal. You will see this operator most often in loops. A for loop, for example, will start looping at a given value, then stop when the counter reaches the upper limit. The following code will stop when the counter (i) reaches 10:

(for int i = 1; i<=10; i++) { }

Greater Than (>) and Greater Than or Equal (>=)

Since we've covered less than (<) and less than or equal (<=), the greater than (>) and greater than or equal to (>=) operators should be apparent.

The following code will evaluate to FALSE and the statements in the if block will process.

if(x > y) {
  printf("%s", "PASS");

On the other hand, this code will evaluate to TRUE:

if(y > x) {
  printf("%s", "PASS");

The >= also works in loops, especially when counting down. Let's flip the previous for loop to start at 10 and count down, stopping at 1:

for(i=10;i>=1;i--) { }

To unlock this lesson you must be a Study.com Member.
Create your account

Register for a free trial

Are you a student or a teacher?
I am a teacher

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Earning College Credit

Did you know… We have over 95 college courses that prepare you to earn credit by exam that is accepted by over 2,000 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it free for 5 days!
Create An Account