Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Equality and Relational Operators of C

August 13, 2013 Posted by: GPA Views: 692 1 comments

Sometimes we must compare some values. C programing language uses Equality and Relational Operators to make a decision in a program. Example if we need to compare a and b and find which of this variable is greater . For this we can use if statement that allows a program to make a decision based on the truth or falsity of a statement of fact called a condition. If the statement is true then event is executed in the body of the "if statement". There are two type of operators which help to create "if statements". They are equality operators and relational operators. The relational and equality operators associate left to right. But the equality operators have a lower level of priority than the relational operators.


In the table you can find all operators using in C.

 Equality operators

C equality or relational operator Example of C condition Meaning of C condition
== a == b a is equal to b
!= a != b

a is not equal to b

 Relational operators

C equality or relational operator Example of C condition Meaning of C condition
> a > b a is greater than b
< a < b a is less than b
>= a >= b a is greater than or equal to b
<= a <= b a is less than or equal to b

Remember that the two symbols in any of the operators ==, !=, >= and <= are written together without spaces, else if you'll try to debug your program, you will have errors.

Example:

 

  
  

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
   int number1;//first number inputed by user
   int number2;//second number inputed by user
   printf("Enter first number: ");
scanf("%d",&number1);//Read first number
   printf("Enter second number: ");
   scanf("%d",&number2);//Read second number
   //If statement
   if(number1>=number2){//open body for if statement
   printf("%d is greater than or equal to %d\n",number1,number2);
   }//end if
   if(number1<=number2){//open body for if statement
printf("%d is less than or equal to %d\n",number1,number2);
   }//end if  
  if(number1>=number2){//open body for if statement
     printf("%d is greater than  %d\n",number1,number2);
   }//end if
  if(number1<=number2){//open body for if statement
       printf("%d is less than  %d\n",number1,number2);
   }//end if
   if(number1==number2){//open body for if statement
     printf("%d is equal to %d\n",number1,number2);
   }//end if
   if(number1!=number2){//open body for if statement
       printf("%d is not equal to %d\n",number1,number2);
   }//end if
   system("PAUSE"); //delay
   return 0;
}

 

Output

1 Comments...

1. Stavr

August 7, 2013, 7:53 am
Avatar

Your material helped me to do my homework. Thank you.

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe