]> git.ozlabs.org Git - ccan/blob - ccan/minmax/_info
minmax: New module, safe min and max macros
[ccan] / ccan / minmax / _info
1 #include <string.h>
2 #include "config.h"
3
4 /**
5  * minmax - typesafe minimum and maximum functions
6  *
7  * The classic implementation of minimum / maximum macros in C can be
8  * very dangerous.  If the two arguments have different sizes, or
9  * different signedness, type promotion rules can lead to very
10  * surprising results.
11  *
12  * This module implements typesafe versions, which will generate a
13  * compile time error, if the arguments have different types.
14  *
15  * Example:
16  *      #include <ccan/minmax/minmax.h>
17  *      #include <stdio.h>
18  *
19  *      int main(int argc, char *argv[])
20  *      {
21  *              printf("Signed max: %d\n", max(1, -1));
22  *              printf("Unsigned max: %u\n", max(1U, -1U));
23  *              return 0;
24  *      }
25  *
26  * Author: David Gibson <david@gibson.dropbear.id.au>
27  * License:  CC0 (Public domain)
28  *
29  * Ccanlint:
30  *      // We need several gcc extensions
31  *      tests_compile_without_features FAIL
32  */
33 int main(int argc, char *argv[])
34 {
35         /* Expect exactly one argument */
36         if (argc != 2)
37                 return 1;
38
39         if (strcmp(argv[1], "depends") == 0) {
40                 printf("ccan/build_assert\n");
41                 return 0;
42         }
43
44         return 1;
45 }