C Programming

MIN() Macro in C Language

Relational operations in C are widely used and can be found in almost every program that are written in this language. There are several operators in this language – the most commonly used are equal to ( = ), greater than ( > ), and less than ( < ). This type of operation is often used in if-conditions. For example, the input condition is the value of the =, >, or < variable to another variable or constant.

These relational operations are very useful. But there are cases where we need to know not only whether one variable is larger or smaller than another, but also to get its value. Although this is easily done with “if” statements and simple relational operations, the C language also provides macros with functions that return the maximum or minimum value between two variables.

In this Linuxhint article, you’ll learn how to use the macro MIN() to find the minimum value of two variables. We’ll show you the syntax, the calling method, and the data type it accepts. Then, we’ll look at a description on how it works and review the expression and formula that this macro applies.

We then apply what we learned in a practical example that includes the code snippets and images that show how to find the minimum with different data types as inputs to the macro MIN().

Syntax of the MIN() Macro in C Language

MIN (a, b)

Description of the MIN() Macro in C Language

The macro MIN() returns the minimum value between the “a” and “b” variables. The expression that is displayed by the macro MIN() is a true/false condition where a relational operation “<” is applied between the “a” and “b” variables. If “a” is less than “b”, “a” is returned. If “b” is less than “a”, “b” is returned.

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

The macro MIN() works with all data types in its inputs and outputs with the only rule that both input variables must be numeric values.

This macro is defined in the “param.h” header in the “sys” folder. To use it, we need to insert it into our code as follows:

#include <sys/param.h>

How to Find the Minimum Between Two Integer Variables with the Macro MIN()

In this example, we create the “a” and “b” variables of type int to which we assign an arbitrary value and from which we find the minimum by calling the macro MIN(). We then output the returned value using the printf() function.

To do this, we include the “stdio.h” and “param.h” headers and open a main() function of type void. In it, we define the “a” and “b” integers and assign them with a random value. We also define the “c” integer to store the result.

Then, we call the macro MIN() and pass “a” and “b” as the input arguments and “c” as the output arguments. We display the returned result by calling the printf() function. The following is the code for this example:

#include <stdio.h>

#include <sys/param.h>

void main(){

  int a =32;

  int b =14;

  int c;

  c = MIN( a, b);

  printf ( "\nThe minimum is %i\n" , c);

}

Next, we see an image with the compilation and execution of this code. As we can see, the macro MIN() returns the value of “b” in this case.

The same happens if we use the variables of the double type.

#include <stdio.h>

#include <sys/param.h>

void main(){

  double a =3;

  double b =1;

  double c;

  c = MIN( a, b);

  printf ( "\nThe minimum of doubles a and b is %f\n" , c);

}


Minimum and Maximum with Floating Point Variables

The macro MIN() is a useful function, but its use isn’t recommended for variables that use the floating point values. To find the minimum of this kind of values, the math library provides a set of functions that is defined in the “math.h” header. This set consists of the fmin(), fminf(), and fminl() functions. Let’s look at the following syntax for each of these functions:

double fmin(double x, double y);
float fminf(float x, float y);
long double fminl(long double x, long double y);

The fmin() function operates on the data of type double (8 bytes) with floating point. The fminf() function works with the data of type float (4 bytes), while fminl() works with the data of type long double (16 bytes). Also, these functions process the non-numeric values (NaN).

Conclusion

In this Linuxhint article, we explained everything you need to know to use the macro MIN() to find the minimum value between two variables. We looked at the syntax and definition of this macro as well as the formula that applies a true/false condition for a “less than” operation (<) between the two input variables.

We then applied the theory that we learned to a practical example using the code snippets and images to show you how to work with different types of data. We also showed you the recommended options to deal with floating point numbers where the use of MIN() is not recommended.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).