Skip to content
C Basics: Sort Integers Without Comparator Overflow

C Basics: Sort Integers Without Comparator Overflow

November 1, 2026

A sorting callback only needs to report ordering. Subtracting two signed integers can overflow before qsort sees the result. Compare the values directly instead.

Prepare a scratch directory

Tested on Linux x86_64 with GCC 14.2.0. Install GCC and the C standard-library development headers, and make gcc available on PATH. You also need Bash, a text editor, and mktemp, rm, and rmdir. No third-party C library is required.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1

Run the example

Save this as qsort-comparator-demo.c:

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

static int compare_integers(const void *left_pointer, const void *right_pointer)
{
    const int left_value = *(const int *)left_pointer;
    const int right_value = *(const int *)right_pointer;
    return (left_value > right_value) - (left_value < right_value);
}

int main(void)
{
    int values[] = {INT_MAX, 7, INT_MIN, 0, 7};
    const size_t count = sizeof values / sizeof values[0];
    qsort(values, count, sizeof values[0], compare_integers);
    printf("Smallest is INT_MIN: %s\n", values[0] == INT_MIN ? "yes" : "no");
    printf("Middle values: %d %d %d\n", values[1], values[2], values[3]);
    printf("Largest is INT_MAX: %s\n", values[count - 1] == INT_MAX ? "yes" : "no");
    return 0;
}

Compile and run it. The && prevents execution if compilation fails:

gcc -std=c17 -Wall -Wextra -Werror -pedantic qsort-comparator-demo.c -o qsort-comparator-demo &&
./qsort-comparator-demo

Observed output:

Smallest is INT_MIN: yes
Middle values: 0 7 7
Largest is INT_MAX: yes

Read the result

Each relational expression is either zero or one, so their difference is only -1, 0, or 1. No subtraction of the original values occurs. The extreme-value checks make the example independent of the platform’s particular int limits.

Apply it carefully

qsort changes the array in place. Equal elements need not retain their original order. A callback must be consistent and must not modify elements or use their changing positions as ordering keys.

The GNU reference documents the relevant interface or language rules.

Clean up

Remove only the source and executable created here, then remove the empty practice directory:

rm -- qsort-comparator-demo.c qsort-comparator-demo
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directory

If the directory is not empty, inspect the remaining contents before removing anything else.

Acknowledgements

This post was created with assistance from AI.

Last updated on