C Less-Than Operator

In C, the Less-Than < operator is a comparison operator used to check whether the left operand is smaller than the right operand. If the left operand is smaller, the operator returns true (1); otherwise, it returns false (0).

The Less-Than < operator is commonly used in conditional statements and loop conditions.


Syntax of the Less-Than Operator

The syntax to use the Less-Than operator is:

</>
Copy
variable1 < variable2

Explanation:

  1. variable1: The first operand to compare.
  2. <: The less-than comparison operator.
  3. variable2: The second operand to compare with the first operand.

The result of the comparison is 1 (true) if variable1 is smaller than variable2, otherwise 0 (false).


    Examples of the Less-Than Operator

    1. Comparing Two Integer Values

    In this example, we will compare two integer values using the Less-Than < operator and print the result.

    main.c

    </>
    Copy
    #include <stdio.h>
    
    int main() {
        int a = 10, b = 20;
    
        if (a < b) {
            printf("%d is less than %d\n", a, b);
        } else {
            printf("%d is not less than %d\n", a, b);
        }
    
        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 “10 is less than 20” is printed.

    Output:

    10 is less than 20

    2. Using Less-Than Operator in a Loop

    In this example, we will use the Less-Than < operator in a while loop condition to print numbers from 1 to 5.

    main.c

    </>
    Copy
    #include <stdio.h>
    
    int main() {
        int i = 1;
    
        while (i < 6) {
            printf("%d ", i);
            i++;
        }
    
        return 0;
    }

    Explanation:

    1. We initialize i to 1.
    2. The while loop runs as long as i < 6.
    3. Inside the loop, i is printed and incremented by 1.
    4. The loop terminates when i reaches 6.

    Output:

    1 2 3 4 5

    3. Using Less-Than Operator with Floating-Point Numbers

    In this example, we will compare two floating-point numbers using the Less-Than < operator.

    main.c

    </>
    Copy
    #include <stdio.h>
    
    int main() {
        float x = 5.5, y = 7.2;
    
        if (x < y) {
            printf("%.1f is less than %.1f\n", x, y);
        } else {
            printf("%.1f is not less than %.1f\n", x, y);
        }
    
        return 0;
    }

    Explanation:

    1. We declare two floating-point variables x and y with values 5.5 and 7.2.
    2. The if condition checks if x < y.
    3. Since 5.5 < 7.2 is true, the message “5.5 is less than 7.2” is printed.

    Output:

    5.5 is less than 7.2

    Conclusion

    In this tutorial, we covered the Less-Than < operator in C:

    1. The Less-Than < operator is used to check if one value is smaller than another.
    2. It returns 1 (true) if the first operand is smaller and 0 (false) otherwise.
    3. It is commonly used in conditional statements and loops.