min()

Description

Calculates the minimum of two numbers.

Syntax

min(x, y)

Parameter Values

  • x: the first number. Allowed data types: any data type.
  • y: the second number. Allowed data types: any data type.

Return Values

  • The smaller of the two numbers.

Example Code

void setup() { Serial.begin(9600); int x = 2; int y = 3; int z = min(x, y); Serial.print("x = "); Serial.println(x); Serial.print("y = "); Serial.println(y); Serial.print("min = "); Serial.println(z); } void loop() {}

The result in Serial Monitor:

COM6
Send
x = 2 y = 3 min = 2
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTES AND WARNINGS:

Perhaps counter-intuitively, max() is often used to constrain the lower end of a variable's range, while min() is used to constrain the upper end of the range.

Because of the way the min() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results

min(a++, 100); // avoid this - yields incorrect results min(a, 100); a++; // use this instead - keep other math outside the function

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES