REW

How Do You Write AC Program To Check If Two Numbers Are Equal?

Published Aug 29, 2025 4 min read
On this page

In C programming, you can check if two numbers are equal using the equality operator ( ==) inside a conditional statement like if...else. This fundamental concept is a cornerstone of decision-making in C, allowing a program to follow different paths based on whether a condition is true or false.

The basic program

This simple program demonstrates the core logic for comparing two integer values.

#include <stdio.h>
int main() {
    int num1, num2;
    // Prompt the user to enter two numbers
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    // Use the == operator in an if-else statement to check for equality
    if (num1 == num2) {
        printf("The two numbers are equal.\n");
    } else {
        printf("The two numbers are not equal.\n");
    }
    return 0;
}

Use code with caution.

Explanation of the code

  • #include <stdio.h>: This line includes the standard input/output library, which is necessary for functions like printf() and scanf().
  • int main(): The main() function is the entry point of every C program.
  • int num1, num2;: This line declares two integer variables, num1 and num2, to store the numbers that the user will input.
  • printf("Enter the first number: ");: This line displays a message on the screen, prompting the user for input.
  • scanf("%d", &num1);: The scanf() function reads the integer value entered by the user and stores it in the variable num1.
  • if (num1 == num2): This is the conditional statement. The == is the equality operator, which compares the values of num1 and num2.
    • If the values are equal, the condition is true, and the code block inside the if statement is executed.
    • If the values are not equal, the condition is false, and the code block inside the else statement is executed.
  • return 0;: This indicates that the program has successfully executed.

Advanced comparisons and best practices

While the == operator works reliably for integer types, it's essential to understand its behavior with other data types and alternative methods.

Comparing floating-point numbers

Comparing floating-point numbers (float or double) directly with == is often unreliable due to the way they are stored in memory. Small rounding errors can cause two numbers that are mathematically equal to appear different.

**Best practice: Compare with a tolerance (epsilon)**To accurately compare floating-point numbers, check if the absolute difference between them is less than a very small number, known as an epsilon (EPSILON).

#include <stdio.h>
#include <math.h> // For the fabs() function
int main() {
    double fnum1, fnum2;
    const double EPSILON = 0.000001; // Define a small tolerance
    printf("Enter the first floating-point number: ");
    scanf("%lf", &fnum1);
    printf("Enter the second floating-point number: ");
    scanf("%lf", &fnum2);
    if (fabs(fnum1 - fnum2) < EPSILON) {
        printf("The two floating-point numbers are approximately equal.\n");
    } else {
        printf("The two floating-point numbers are not equal.\n");
    }
    return 0;
}

Use code with caution.

Comparing different numeric types

C automatically handles conversions when comparing different numeric types (e.g., int and long). The smaller type is promoted to the larger type before the comparison is performed. However, comparing signed and unsigned integers can lead to unexpected results if a negative number is involved, so this should be handled carefully.

Alternative comparison method using the XOR operator

For integers, you can use the bitwise XOR operator (^) to check for equality without using ==. The XOR of two numbers is 0 if and only if the numbers are the same.

#include <stdio.h>
int main() {
    int num1, num2;
    printf("Enter the first number: ");
    scanf("%d", &num1);
    printf("Enter the second number: ");
    scanf("%d", &num2);
    // XOR of two equal numbers is 0
    if ((num1 ^ num2) == 0) {
        printf("The two numbers are equal (using XOR).\n");
    } else {
        printf("The two numbers are not equal (using XOR).\n");
    }
    return 0;
}

Use code with caution.

Common pitfalls and reminders

  • Assignment vs. equality: A single equals sign (=) is the assignment operator, which assigns a value to a variable. A double equals sign (==) is the equality operator, which tests for equality. Using = inside an if condition is a common error that can lead to bugs, as it will always evaluate to true for any non-zero value.
  • Use curly braces: While optional for single-line if or else blocks, always using curly braces {} improves code readability and prevents future bugs if you add more statements to a conditional block.
  • Operator precedence: Remember that == has lower precedence than arithmetic operators. Parentheses can be used to ensure the correct order of operations in complex expressions.
Enjoyed this article? Share it with a friend.