C llrint() Function

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

Definition

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

You pass one double. The result is a long long, not a floating-point type. Include <math.h>. On GCC, link with -lm. Prefer lrint() when a long is wide enough.

Unlike llround(), which uses a fixed halfway rule (away from zero), llrint() follows the floating-point environment. Changing the rounding mode with fesetround() can change the result for the same input.

Syntax

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

C
llrint(2.5);
CallWhat it does
llrint(2.5)Rounds using the current mode; often 2 (ties to even) in the default mode.

Include <math.h>. On GCC, link with -lm. The result type is long long, so print it with %lld.

Library declarations
C
long long llrint(double x);
long long llrintf(float x);
long long llrintl(long double x);

Parameters

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

Return Value

Return ValueDescription
long longThe rounded integer value. If the result is out of range, the call may raise a floating-point exception.

C Examples

Example 1 — Basic Rounding to long long

Round a few doubles with the default rounding mode (usually nearest, ties to even).

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

int main(void) {
    printf("%lld\n", llrint(3.2));
    printf("%lld\n", llrint(3.8));
    printf("%lld\n", llrint(-1.5));
    return 0;
}

How It Works

  1. Include math.h. That declares llrint.
  2. Below halfway. 3.2 rounds toward 3.
  3. Above halfway. 3.8 rounds toward 4.
  4. Negative halfway. With ties-to-even, -1.5 often becomes -2.

Example 2 — Store the Result

Keep the rounded value in a long long variable for later math or formatting.

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

int main(void) {
    double price = 19.75;
    long long cents = llrint(price * 100.0);

    printf("cents = %lld\n", cents);
    return 0;
}

How It Works

  1. Scale first. Multiply by 100.0 to work in cents.
  2. Round with llrint. The default mode picks the nearest integer.
  3. Type is long long. You can store large money or counter values safely.
  4. Print with %lld. That is the usual format for long long.

Example 3 — Compare with llround

See how halfway cases can differ from llround(), which always moves half away from zero.

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

int main(void) {
    double x = 2.5;
    printf("llrint  = %lld\n", llrint(x));
    printf("llround = %lld\n", llround(x));
    return 0;
}

How It Works

  1. Same input. Both functions see 2.5.
  2. llrint uses mode. Default ties-to-even often yields 2.
  3. llround is fixed. Halfway moves away from zero, so the result is 3.
  4. Pick on purpose. Choose based on whether you want mode control or a fixed rule.

Frequently Asked Questions

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

Next: llround()

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

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