Site Tools


wiki:h-limits

Table of Contents

<limits.h>

<limits.h> provides platform-specific integer type limits: INT_MAX, INT_MIN, UINT_MAX, LONG_MAX, etc. Use these when writing portable code that works correctly regardless of whether integers are 32 or 64 bits.

Never hardcode values like 2147483647 directly; use the macros instead.

Example

This example queries integer limits and demonstrates overflow checking.

// compile: gcc -o limitsexample limitsexample.c
// run: ./limitsexample
// description: query integer limits and check for overflow
 
#include <limits.h>
#include <stdio.h>
 
int main() {
    printf("INT_MAX = %d\n", INT_MAX);
    printf("INT_MIN = %d\n", INT_MIN);
    printf("UINT_MAX = %u\n", UINT_MAX);
    printf("LONG_MAX = %ld\n", LONG_MAX);
 
    int a = INT_MAX, b = 1;
    if (a > INT_MAX - b)
        printf("INT_MAX + 1 would overflow\n");
 
    unsigned int u = UINT_MAX;
    printf("UINT_MAX + 1 = %u (wraps to 0)\n", u + 1);
 
    return 0;
}
wiki/h-limits.md · Last modified: by 127.0.0.1