C Not-Equal Operator

In C, the Not-Equal != operator is a comparison operator used to check whether two values are different. If the values are not equal, the operator returns true (1); otherwise, it returns false (0).

The Not-Equal != operator is commonly used in conditional statements like if and loop conditions.


Syntax of the Not-Equal Operator

The syntax to use the Not-Equal operator is:

</>
Copy
variable1 != variable2

Explanation:

  1. variable1: The first operand to compare.
  2. !=: The not-equal comparison operator.
  3. variable2: The second operand to compare with the first operand.

The result of the comparison is 1 (true) if the operands are not equal, otherwise 0 (false).


    Examples of the Not-Equal Operator

    1. Using Not-Equal to Compare Two Integers

    In this example, we will compare two integer values using the Not-Equal != operator and print the result.

    main.c

    </>
    Copy
    #include <stdio.h>
    
    int main() {
        int a = 10, b = 20;
    
        if (a != b) {
            printf("Numbers are not equal.\n");
        } else {
            printf("Numbers are equal.\n");
        }
    
        return 0;
    }

    Explanation:

    1. We declare two integer variables a and b, initialized to 10 and 20.
    2. The if condition checks if a != b.
    3. Since 10 != 20 is true, the message “Numbers are not equal.” is printed.

    Output:

    Numbers are not equal.

    2. Using Not-Equal with Different Data Types

    In this example, we will compare a character and an integer using the Not-Equal != operator.

    main.c

    </>
    Copy
    #include <stdio.h>
    
    int main() {
        char ch = 'A';
        int num = 65;
    
        if (ch != num) {
            printf("Character and number are not equal.\n");
        } else {
            printf("Character and number are equal.\n");
        }
    
        return 0;
    }

    Explanation:

    1. We declare a character variable ch with value ‘A’ and an integer num with value 65.
    2. The ASCII value of ‘A’ is 65, so ch != num evaluates to false.
    3. Since the condition is false, “Character and number are equal.” is printed.

    Output:

    Character and number are equal.

    3. Using Not-Equal to Control a Loop

    In this example, we will use the Not-Equal != operator to terminate a while loop when a specific condition is met.

    main.c

    </>
    Copy
    #include <stdio.h>
    
    int main() {
        int i = 1;
    
        // Loop runs until i is not equal to 5
        while (i != 5) {
            printf("i = %d\n", i);
            i++;
        }
    
        return 0;
    }

    Explanation:

    1. We initialize i to 1.
    2. The while loop runs as long as i != 5.
    3. Inside the loop, i is printed and incremented by 1.
    4. The loop terminates when i becomes 5.

    Output:

    i = 1
    i = 2
    i = 3
    i = 4

    Conclusion

    In this tutorial, we covered the Not-Equal != operator in C:

    1. The Not-Equal != operator checks if two values are different.
    2. It returns 1 (true) if the values are not equal and 0 (false) if they are equal.
    3. It is used in conditional statements and loops to control program flow.

    The Not-Equal != operator is essential for decision-making and loop control in C programming.