---
name: "SDL_clamp"
section: "3"
platform: "linux"
package: "libsdl3-doc"
version: "3.2.10+ds-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/SDL_clamp"
---

# SDL\_clamp(3)

Return a value clamped to a range.

## NAME

SDL\_clamp - Return a value clamped to a range.

## HEADER FILE

Defined in SDL3/SDL\_stdinc.h

## SYNOPSIS

```
#include "SDL3/SDL.h"
```

**#define SDL\_clamp(x, a, b) (((x) \< (a)) ? (a) : (((x) \> (b)) ? (b) : (x)))**

## DESCRIPTION

If **x** is outside the range a values between **a** and **b** , the returned value will be **a** or **b** as appropriate. Otherwise, **x** is returned.

This macro will produce incorrect results if **b** is less than **a** .

This is a helper macro that might be more clear than writing out the comparisons directly, and works with any type that can be compared with the **\<** and **\>** operators. However, it double-evaluates all its parameters, so do not use expressions with side-effects here.

## RETURN VALUE

Returns x, clamped between a and b.

## THREAD SAFETY

It is safe to call this macro from any thread.

## AVAILABILITY

This macro is available since SDL 3.2.0.
