Not Equal to (! =) in C
In C programming, Relational operators are used to compare two items by building a relationship between the two. The result of the relational operator is 1 or 0. The true result is indicated by 1 and the False by 0.
Types of Relational Operators
There are different types of Relational Operators:
Symbol | Name | Use |
== | Equals to | It tests the equality among values. |
!= | Not equals to | It is used to determine whether both values are unequal. |
< | Less than | It checks if the first value is smaller than the other. |
> | Greater than | It checks if the first value is greater than the other. |
<= | Less than or equal to | If the first value is smaller than or equal to the second is determined. |
>= | Greater than or equal to | It determines whether the first number is greater than or equal to the second. |
Not Equals to Operator in Relational Operators
The Not Equals To operator is denoted by (! =). It takes two operands and compares both values to check the inequality. If both are not equal, the condition becomes true and returns 1 as a result, and if both the value are equal, then the condition becomes false and will return 0 as a result.
It works definitely contrary to the equals to operator. If statement is used to check the inequality using not equals to operator.
Syntax of Not Equals to Operator
opr1 != opr2
where opr1 = operand 1, which stores the value
opr2 = operand 2, which stores another value.
The operator not equals to is used with the conditional statements and loops.
With conditional statements, this operator is used as:
if(opr1 != opr2)
{
//statements
}
else
{
//statements
}
In this, the conditional statement (if) compares the condition and checks the inequality of both numbers. If both are not equal, the statement under the if block will print; otherwise, it will go to the else block.
With loops, the not equal to operator is used as:
for(i=5; i != 0; i--)
{
//statements
}
In this, the for loop uses the not equal to operator in the condition statement and iterates a loop until the value of i becomes 0. Initially, the value of i is 5, and then it will check the statement until the value of i is not equal to 0 and then decrease its value by one.
Program to illustrate the use of Not Equal to Operator in C
1. Using conditional statement
CODE
#include <stdio.h>
#include<conio.h>
void main()
{
int a = 3, b = 5;
if(a != b)
{
printf("a is not equal to b");
}
else
{
printf("a is equal to b");
}
}
OUTPUT
![Not Equal to (! =) in C](https://static.tutorialandexample.com/c/not-equal-to-in-c1.png)
In this code, two variables, a and b, are taken and are assigned with values 3 and 5, respectively. Then inequality of both values is checked using a conditional statement. As a is not equal to b thus, the statement under the if statement is printed.
2. Taking input from users
CODE
#include <stdio.h>
#include<conio.h>
void main()
{
int x , y;
printf("Enter value of x:\n");
scanf("%d", &x);
printf("Enter value of y:\n");
scanf("%d", &y);
if(x != y)
{
printf("x is not equal to y");
}
else
{
printf("x is equal to y");
}
}
OUTPUT
![Not Equal to (! =) in C](https://static.tutorialandexample.com/c/not-equal-to-in-c2.png)
In this code, two variables, x, and y, are taken, and their values are taken from the user. The values entered by the user will be checked through the if statement; if both the values are not equal, then the statement under the condition will be printed. As the user has entered 22 and 33, which are not equal thus, the statement ‘x is not equal to y’ is printed.
3. Printing reverse counting using For loop.
CODE
#include <stdio.h>
#include<conio.h>
void main()
{
int i, n=0;
printf("Printing Reverse Count:\n");
for (i = 10; i != n; i--)
{
printf("%d\n", i);
}
}
OUTPUT
![Not Equal to (! =) in C](https://static.tutorialandexample.com/c/not-equal-to-in-c3.png)
We have printed a reverse counting from 10 to 1 in this code. We have started a loop in which i is assigned with value 10, and n is assigned with value 0. When the loop starts, it will check for the condition which states iterate the loop until i is not equal to n and decreases its value by 1 after each iteration; thus, it iterates and prints from 10 to 1 until the value of i becomes 0.
4. Displaying the logical values using Not Equals to operator.
CODE
#include <stdio.h>
#include<conio.h>
void main()
{
int num1 = 15, num2 = 20;
int x = 2, y = 2;
printf("num1 != num2: %d", (num1 != num2));
if(num1 != num2)
{
printf("\nnum1 is not equal to num2");
}
else
{
printf("\nmum1 is equal to num2");
}
//CASE 2
printf("\n\nCASE2\n\n");
printf("x != y: %d", (x != y));
if(x != y)
{
printf("\nx is not equal to x");
}
else
{
printf("\nx is equal to y");
}
}
OUTPUT
![Not Equal to (! =) in C](https://static.tutorialandexample.com/c/not-equal-to-in-c4.png)
Explanation
In this code, we have taken 2 cases that print logical values of the not equal to operator. In the first case, two different values are compared; if the condition of the not equal operator is true, it returns 1, thus, returning 1 with the message “num1 is not equal to num2”.
In the other case, both values are the same; thus, it returns 0 as the condition is false, and the message prints “x is equal to y.”