Back to C Programming
2026-02-107 min read

Greater-than operator (C Programming)

Learn Greater-than operator (C Programming) step by step with clear examples and exercises.

Why This Matters

The greater-than operator (>) is a fundamental component of C programming that allows developers to compare values and make decisions based on those comparisons. A thorough understanding of this operator can help you write more efficient code, avoid common mistakes, and tackle real-world coding challenges effectively. In this lesson, we will delve deeper into the basics of the greater-than operator, provide extensive examples of its usage, and highlight some common pitfalls to watch out for.

Prerequisites

To fully grasp the concepts presented in this lesson, you should have a solid understanding of C programming fundamentals such as variables, data types, control structures like if-else statements, loops, functions, and arrays. If you're new to C programming or need a refresher, consider checking out our previous lessons on these topics before diving into the greater-than operator.

Core Concept

Definition and Usage

The greater-than operator (>) is a binary operator in C that tests whether the left operand is greater than the right operand. If the condition is true, it returns 1; otherwise, it returns 0. Here's an example:

int a = 5;
int b = 3;
if (a > b) {
printf("a is greater than b\n");
}

In this code snippet, a is indeed greater than b, so the message "a is greater than b" will be printed.

Operator Precedence and Associativity

Like other operators in C, the greater-than operator follows a specific order of precedence when multiple operators appear in an expression. You can use parentheses to group expressions and control the order of evaluation if needed. The greater-than operator has higher precedence than arithmetic operators like addition and subtraction but lower precedence than relational operators like equality (==) and inequality (!=).

When to Use the Greater-Than Operator

You can use the greater-than operator in various situations, such as:

  1. Comparing two numeric values to determine which is larger
  2. Checking if a variable's value exceeds a certain threshold
  3. Implementing sorting algorithms that require comparisons between elements
  4. Writing conditional statements for decision-making in your programs
  5. Validating user input against expected ranges
  6. Comparing floating-point numbers, keeping in mind the potential issues related to precision and rounding errors
  7. Comparing character variables as integers (ASCII values)
  8. Comparing pointers when checking if one pointer is greater than another in memory
  9. Using the greater-than operator in logical expressions for more complex conditional statements

Worked Example

Let's consider a simple example where we compare the ages of two people and determine who is older:

#include <stdio.h>

int main() {
int person1Age = 25;
int person2Age = 30;

if (person1Age > person2Age) {
printf("Person 1 is older.\n");
} else {
printf("Person 2 is older.\n");
}

return 0;
}

In this example, we've defined the ages of two people and used an if-else statement to compare their ages and print out who is older. When you run this code, it will output "Person 2 is older."

Extended Worked Example: Comparing Floating-Point Numbers

When comparing floating-point numbers, be aware of potential issues related to precision and rounding errors. Let's consider an example where we compare two floating-point numbers:

#include <stdio.h>

int main() {
float a = 3.14159265358979323846f; // Approximate value of pi
float b = 3.141592653589793; // Slightly different value for demonstration purposes

if (a > b) {
printf("a is greater than b\n");
} else {
printf("b is greater than a\n");
}

return 0;
}

In this example, the difference between a and b is extremely small but not exactly zero. Due to floating-point precision limitations, the comparison may yield unexpected results:

b is greater than a

To avoid such issues, consider using functions like fabs() or isgreater() from the `` library when comparing floating-point numbers:

#include <stdio.h>
#include <math.h>

int main() {
float a = 3.14159265358979323846f; // Approximate value of pi
float b = 3.141592653589793; // Slightly different value for demonstration purposes

if (isgreater(a, b)) {
printf("a is greater than b\n");
} else {
printf("b is greater than a\n");
}

return 0;
}

In this revised example, the isgreater() function from the ` library will correctly determine that a is indeed greater than b`.

Common Mistakes

Forgetting to Include Necessary Headers

Remember to include the necessary header files at the beginning of your C programs:

#include <stdio.h>

Without this line, you won't be able to use functions like printf().

Incorrect Use of Operators

Be sure to use the correct operator for comparison. For example, if you want to check if a number is greater than or equal to another number, use the equality operator (==) instead of the greater-than operator (>).

Comparing Different Data Types

When comparing values with the greater-than operator, ensure that both operands have the same data type. Otherwise, you may encounter unexpected results due to implicit type conversions.

Neglecting Edge Cases

It's important to consider edge cases when using comparison operators like the greater-than operator. For example, in the age comparison example above, we didn't handle the case where both people have the same age. You can modify the code to include this edge case:

if (person1Age > person2Age) {
printf("Person 1 is older.\n");
} else if (person1Age < person2Age) {
printf("Person 2 is older.\n");
} else {
printf("Both people have the same age.\n");
}

Using the Greater-Than Operator for Inappropriate Comparisons

Avoid using the greater-than operator in situations where it's not suitable, such as comparing strings or pointers with different memory addresses. Instead, use functions like strcmp() or memcmp() when working with strings and memchr() or memcmp() when working with pointers.

Practice Questions

  1. Write a program that asks for two numbers as input and determines which is larger.
  2. Modify the previous example to handle cases where both people have the same age.
  3. Write a program that sorts three integers in ascending order using the greater-than operator.
  4. Write a program that calculates the average of three numbers and checks if it's greater than 20.
  5. Write a program that determines whether a given number is within a specific range (e.g., between 10 and 20).
  6. Write a program that compares two floating-point numbers and checks if the difference between them is less than a specified tolerance value.
  7. Write a program that reads an array of integers and finds the maximum value using the greater-than operator.
  8. Write a program that reads an array of strings and sorts them in lexicographical order using the strcmp() function.
  9. Write a program that checks if a given character is a vowel or consonant.
  10. Write a program that determines whether a given string is a palindrome (reads the same forwards and backwards).

FAQ

Q: Can I use the greater-than operator for string comparisons?

A: No, you should use the string comparison functions like strcmp() instead of the greater-than operator when comparing strings.

Q: What happens if I compare a floating-point number with an integer using the greater-than operator?

A: When comparing a floating-point number with an integer, the integer will be implicitly converted to a float before the comparison takes place. However, this can lead to unexpected results due to rounding errors or loss of precision. To avoid these issues, it's best to use floating-point variables for both operands when comparing floating-point numbers.

Q: How do I compare two character variables using the greater-than operator?

A: In C, character variables are treated as integers, so you can directly compare them with the greater-than operator. However, keep in mind that ASCII values are used for comparison, not the actual characters themselves. For example, 'A' (65) is considered greater than 'a' (97).

Q: How do I handle negative numbers when using the greater-than operator?

A: When comparing two negative numbers with the greater-than operator, the one with a smaller absolute value will be considered smaller. If you want to compare only positive values or absolute values, consider using absolute value functions like fabs() for floating-point numbers or taking the absolute value manually for integers.

Q: Is it possible to use the greater-than operator in logical expressions?

A: Yes, you can use the greater-than operator as part of a larger logical expression. For example:

if ((a > b) && (c > d)) {
// Your code here
}

In this example, both conditions (a > b) and (c > d) must be true for the if-statement to execute.