HOWTO · C

MIN and MAX Function in C

One can use macros or loops to implement the MIN and MAX function in C.

On this page

To find the smaller or larger of two values in C, define a MIN or MAX macro, or write a function. Standard C does not provide general built-in MIN and MAX functions. For an array, initialize the result from its first element and scan the remaining elements.

Are MIN and MAX built into C?

C’s standard library does not define general-purpose MIN and MAX functions. These names are commonly supplied by a project as macros or ordinary functions. A function is easier to reason about when arguments have side effects; a simple macro can be concise for values that are evaluated safely.

The #define directive creates a preprocessor macro. When the preprocessor sees a macro call, it substitutes the replacement text before the compiler processes the program. A function call, by contrast, invokes compiled code with arguments evaluated according to C’s function-call rules.

Define MIN and MAX macros for two values

For two integer expressions with no side effects, parenthesize each parameter and the full replacement expression. This protects the intended grouping when a caller passes an expression such as a + b.

#include <stdio.h>

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

int main(void) {
  printf("Min = %d\n", MIN(0, 10));
  printf("Max = %d\n", MAX(0, 10));
  return 0;
}
Min = 0
Max = 10

The conditional operator ?: selects the expression after ? when the comparison is true and the expression after : otherwise. Parentheses handle precedence, but they do not prevent repeated evaluation: in MIN(a, b), the selected argument appears again in the replacement text. Avoid arguments such as i++ or function calls that change state. This macro form is best for simple values; use a function when arguments may have side effects.

Use a GNU C macro to evaluate arguments once

GNU C supports statement expressions and typeof (spelled __typeof__ in this example). The macro below saves each argument in a temporary, then compares those values. Each argument expression is evaluated once in its initializer. This is a GNU C extension, so it is not portable ISO C and may need a compiler option that enables GNU extensions.

#include <stdio.h>

#define MAX(x, y)                    \
  ({                                 \
    __typeof__(x) _max_x = (x);      \
    __typeof__(y) _max_y = (y);      \
    _max_x > _max_y ? _max_x : _max_y; \
  })

#define MIN(x, y)                    \
  ({                                 \
    __typeof__(x) _min_x = (x);      \
    __typeof__(y) _min_y = (y);      \
    _min_x < _min_y ? _min_x : _min_y; \
  })

int main(void) {
  int a = 4;
  int b = 9;
  printf("Min = %d\n", MIN(a++, b));
  printf("Max = %d\n", MAX(a, b));
  return 0;
}
Min = 4
Max = 9

This pattern is useful for ordinary scalar expressions whose types can be compared with < or >. It is not a universal type-generic function: incompatible types, pointers without a meaningful ordering, and types that do not support these operators need a different design. The conditional expression also follows C’s usual type-conversion rules. For portable code, prefer a typed function or write separate functions for the types you support.

Find the minimum and maximum in an array

For an array, keep the current minimum and maximum and update them as you scan. Initialize both from the first item so the result is based on an actual array value. The example uses the same values as the original array example and scans the array once.

#include <stdio.h>
#include <stddef.h>

void find_min_max(const int values[], size_t length, int *minimum, int *maximum) {
  *minimum = values[0];
  *maximum = values[0];

  for (size_t i = 1; i < length; ++i) {
    if (values[i] < *minimum) {
      *minimum = values[i];
    }
    if (values[i] > *maximum) {
      *maximum = values[i];
    }
  }
}

int main(void) {
  int values[] = {6, 3, 15, 9, 10, 50, 99, 11};
  size_t length = sizeof(values) / sizeof(values[0]);
  int minimum;
  int maximum;

  find_min_max(values, length, &minimum, &maximum);
  printf("Max number = %d\n", maximum);
  printf("Min number = %d\n", minimum);
  return 0;
}
Max number = 99
Min number = 3

This function requires a nonempty array and valid result pointers. The caller passes the length because an array parameter is adjusted to a pointer; inside find_min_max, sizeof(values) measures the pointer, not the original array. Here, sizeof computes the element count in main, where values is still an array: divide its total size by the size of one element.

For floating-point data, use a consistent type for the array, the minimum and maximum variables, and the function parameters. Match the printf conversion to the promoted argument type; for a double, use %f. If the function should accept multiple numeric types, write appropriately typed functions or use a carefully designed abstraction rather than changing only int to float.