C lrint() Function

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

Definition

In C, lrint() rounds a double to the nearest integer using the current rounding mode, then returns that value as a long. The default mode is usually round-to-nearest, ties to even.

You pass one double. The result is a long. Include <math.h>. On GCC, link with -lm. For a wider integer result, use llrint().

lrint() is the long twin of llrint(). Unlike lround(), it does not hard-code a half-away-from-zero rule.

Syntax

Pass the floating-point number. Start with this call.

C
lrint(2.5);
CallWhat it does
lrint(2.5)Rounds with the current mode; often 2 under default ties-to-even.

Include <math.h>. On GCC, link with -lm. Print with %ld on most systems.

Library declarations
C
long lrint(double x);
long lrintf(float x);
long lrintl(long double x);

Parameters

ParameterDescription
xRequired. The double to round according to the current rounding mode.

Return Value

Return ValueDescription
longThe rounded integer value as a long.

C Examples

Example 1 — Basic Rounding to long

Round a few values with the default mode.

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

int main(void) {
    printf("%ld\n", lrint(4.1));
    printf("%ld\n", lrint(4.9));
    printf("%ld\n", lrint(-3.5));
    return 0;
}

How It Works

  1. Include math.h. That declares lrint.
  2. Below halfway. 4.1 becomes 4.
  3. Above halfway. 4.9 becomes 5.
  4. Negative half. Ties-to-even often maps -3.5 to -4.

Example 2 — Round a Scaled Index

Turn a floating ratio into a long index with mode-aware rounding.

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

int main(void) {
    double ratio = 0.74;
    long idx = lrint(ratio * 10.0);

    printf("idx = %ld\n", idx);
    return 0;
}

How It Works

  1. Scale the ratio. Multiply by 10 to map into index space.
  2. Round with lrint. Default mode picks the nearest long.
  3. Use as an index. A long fits many array sizes on 64-bit hosts.
  4. Mind the range. Always clamp if the index can go out of bounds.

Example 3 — Compare with lround

Show a halfway case where lrint and lround often disagree.

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

int main(void) {
    double x = 2.5;
    printf("lrint  = %ld\n", lrint(x));
    printf("lround = %ld\n", lround(x));
    return 0;
}

How It Works

  1. Shared halfway. Both see 2.5.
  2. lrint uses mode. Default ties-to-even often yields 2.
  3. lround is fixed. Half away from zero yields 3.
  4. Choose on purpose. Mode control versus a fixed classroom rule.

Frequently Asked Questions

lrint() rounds a double using the current rounding mode and returns a long.
x is required. It is the double to round.
A long holding the rounded integer value.
Include <math.h>. On GCC, link with -lm.
lrint() follows the current rounding mode. lround() uses fixed half-away-from-zero.

Next: lround()

Round a floating-point value to long with a fixed halfway rule.

lround() 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