EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Relational Operators in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Relational Operators in C

relational operators in c

Introduction to Relational Operators in C

Relational operators are those which are part of Binary operators. These operators are generally used in the comparison between two values or conditions. This comparison condition drives us to the Boolean expression values by which the code written is accordingly executed.

Types of Relational Operators in C Language

Below are the different types of relational operators in C language:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Greater than (>)

This operator checks and executes the code according to the ‘greater than’ functionality. It checks if the left side operands or the right side operands are greater and executes the condition that way.

Code:

#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a>b)
{
printf(" a’s value is greater than b’s value");
}
else
{
printf(" b’s value is greater than a’s value");
}
}

Output:

relational operators in c1

The program compares if the value of ‘a’ is greater or if the value of ‘b’ is greater and executes if the condition to give us the output.

2. Less than (<)

This operator is similar to that of greater than the operator that is discussed above. The only difference is the output is expected to give us the operand whose value is less than that of the other one.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

relational operators inc less thanJPG

The same example values as of above are used to make a better understanding on how these greater- than and less-than operators work according to the conditions.

3. Greater than and equal to (>=)

This operator not only satisfies the ‘greater than’ condition but also includes if the left side operand is equal to the right side operand or not.

Code:

#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a>b)
{
printf("Value of a is greater than b");
}
else if(a>=b)
{
printf("Value of a is equal to b");
}
else
{
printf("Value of b is greater than a");
}
}

Output:

relational operators greator than equal to

Here, we have used both the conditions and gave an example for the values that are equal to each other. So, we have got equal conditions executed.

4. Less than and equal to (<)

This operator is similar to the above mentioned ‘greater than and equal to’ operator. The only difference is with the operators ‘less than’ and ‘greater than’.

Code:

#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a<b)
{
printf("Value of a is less than b");
}
else if(a<=b)
{
printf("Value of a is equal to b");
}
else
{
printf("Value of b is less than a");
}
}

Output:

less than equal to

As we are having equal conditions in priority with ‘less than’ operator, we had given the example accordingly.

5. Double equal to (==)

This operator only defines if the left side operand is equal to the right side operand. This also means that it is not going to consider the conditions ‘greater than’ or ‘less than’ but only the equal condition.

Code:

#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a==b)
{
printf("Value of a is equal to b");
}
else
{
printf("Value of a is not equal to b");
}
}

Output:

double equal to

For comparing the equal condition between left and right operand values, we should use the ‘double equal to’ operator for it.

6. Not Equal to (!=)

This operator is for doing the opposite functionality of that mentioned above. This is for making the condition between the left and right operands should not be equal to each other.

Code:

#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if(a!=b)
{
printf("Value of a is not equal to b");
}
else
{
printf("Value of a is equal to b");
}
}

Output:

not equal to

Below given is a random example used by utilizing all the relational operators:

#include <stdio.h>
int main()
{
int a,b;
printf(" Enter value for a: ");
scanf("%d", &a);
printf(" Enter value for b: ");
scanf("%d", &b);
if((a-b) > b)
{
printf(" Subtracted value of a and b is greater than b");
}
else if((a-b) < b)
{
printf(" Subtracted value of a and b is less than b");
}
else if((a==b))
{
printf(" Value of a and b are equal to each other");
}
else if((a+5)!=(b+6))
{
printf(" Value of sum of a and 5 is not equal to sum of b and 6");
}
else if((a*b) >= (b*b))
{
printf(" Multiplied value of a and b is greater than or equal tovalue of square of b");
}
else if((a*b) <= (b*b))
{
printf(" Multiplied value of a and b is less than or equal to value of square of b");
}
else
{
printf(" Tried all the relational operators");
}
}

Output:

reltn oprtr 2

Conclusion

So, this is how we can use different relational operators. These operations play a very important role in any programming language. And here we had done the same in C programming language.

Recommended Articles

This has been a guide to Relational Operators in C. Here we discuss the introduction and different types of relational operators in C. you may also have a look at the following articles to learn more –

  1. Python Comparison Operators
  2. Comparison Operators in PHP
  3. Relational Operators in C++
  4. Left Shift Operator in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More