• Home
  • 3.9 Equality Operators

3.9 Equality Operators

View Categories

3.9 Equality Operators

< 1 min read

Table of Contents

Equality operators compare two values to determine whether they are equal or different. The == operator tests for equality, while the != operator tests for inequality.

#include <stdio.h>

int main()
{
    int a = 10;
    int b = 20;

    printf("a == b : %d\n", a == b);
    printf("a != b : %d\n", a != b);

    return 0;
}

Example Output #

a == b : 0
a != b : 1

Explanation #

The equality operators are:

Operator Meaning
== Equal to
!= Not equal to

The expression:

a == b

checks whether a and b have equal values. Since 10 and 20 are different, the expression produces 0.

The expression:

a != b

checks whether the values are different. Since they are different, it produces 1.

The assignment operator = is different from the equality operator ==. = assigns a value, while == compares values.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *