C isgreater() Macro

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, isgreater() is a <math.h> comparison macro. It answers a yes/no question: x is greater than y. A true answer is a non-zero value (often printed as 1). A false answer is 0.

Unlike a raw x > y compare, isgreater() is designed so that NaN does not raise an invalid floating-point exception. If either value is NaN, the answer is false (except for special cases covered by related macros such as isunordered).

Pass two real floating values. Include <math.h>. Print the result with %d after casting to int if you want a clear 0 or 1 on the screen.

Syntax

Compare two floating-point values. Start with this call.

C
isgreater(x, y);
CallWhat it does
isgreater(5.0, 3.0)Returns non-zero because 5.0 is greater than 3.0.
isgreater(2.0, 9.0)Returns 0 because 2.0 is not greater than 9.0.
isgreater(NAN, 1.0)With NaN involved, the result is 0 (false) without raising an invalid exception from this macro.

Include <math.h>. On GCC, link with -lm. Prefer this macro over x > y when either side might be NaN and you do not want an invalid exception.

Library declarations
C
#define isgreater(x, y) /* implementation-defined macro */

Parameters

ParameterDescription
xRequired. Left-hand floating value (float, double, or long double).
yRequired. Right-hand floating value to compare against.

Return Value

Return ValueDescription
Non-zero (true)When x is greater than y.
0 (false)When the relation is not true, including many NaN cases.

C Examples

Example 1 — Basic Greater-Than Check

Print 1 when the left value is larger, otherwise print 0.

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

int main(void) {
    printf("%d\n", (int)isgreater(5.0, 3.0));
    printf("%d\n", (int)isgreater(2.0, 9.0));
    printf("%d\n", (int)isgreater(4.0, 4.0));
    return 0;
}

How It Works

  1. Include math.h. That provides the isgreater macro.
  2. True case. 5.0 is greater than 3.0, so you see 1.
  3. False case. 2.0 is smaller than 9.0, so you see 0.
  4. Equal values. Equal numbers are not greater, so the third line is also 0.

Example 2 — NaN Does Not Raise Invalid

Compare with NaN. The macro returns 0 and stays quiet about invalid exceptions.

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

int main(void) {
    double a = NAN;
    double b = 10.0;

    printf("%d\n", (int)isgreater(a, b));
    printf("%d\n", (int)isgreater(b, a));
    return 0;
}

How It Works

  1. Make a NaN. NAN is not ordered with normal numbers.
  2. Both directions fail. Neither a > b nor b > a is true when NaN is involved.
  3. Print 0. Both calls print 0.
  4. Why it matters. This path avoids the invalid exception a plain > can raise with NaN.

Example 3 — Guard a Branch Safely

Use the macro before choosing a larger reading from two sensors.

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

int main(void) {
    double left = 12.5;
    double right = 8.25;

    if (isgreater(left, right)) {
        printf("use left\n");
    } else {
        printf("use right\n");
    }

    return 0;
}

How It Works

  1. Store two readings. left is 12.5 and right is 8.25.
  2. Ask isgreater. The macro checks whether left is strictly larger.
  3. Take a branch. Because the check is true, the program prints use left.
  4. Safer pattern. If a future reading becomes NaN, this check still fails cleanly without an invalid exception.

Frequently Asked Questions

isgreater() is a math.h comparison macro. It checks whether x is greater than y.
x and y are required floating values. They are the two numbers you want to compare.
A non-zero int-like result for true, or 0 for false. Beginners often print it as 0 or 1.
Include <math.h>. On GCC, link with -lm.
The macro is quieter with NaN: it does not raise an invalid floating-point exception the way a plain relational operator can. Use related macros like isless / isgreaterequal when you need the other relation.

Next: isgreaterequal()

Check whether one floating value is greater than or equal to another.

isgreaterequal() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful