Browse Source Download (without any required ccan dependencies)
minmax
typesafe minimum and maximum functions
David Gibson <david@gibson.dropbear.id.au>
The classic implementation of minimum / maximum macros in C can be very dangerous. If the two arguments have different sizes, or different signedness, type promotion rules can lead to very surprising results.
This module implements typesafe versions, which will generate a compile time error, if the arguments have different types.
#include <ccan/minmax/minmax.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Signed max: %d\n", max(1, -1));
printf("Unsigned max: %u\n", max(1U, -1U));
return 0;
}
CC0 (Public domain)