max
max(mixed $value, mixed ...$values): mixed
Returns the largest of several values.
C C23
C23
#include <stdio.h>
int main(void) {
int xs[] = {3, 7, 2};
size_t n = sizeof xs / sizeof xs[0];
int best = xs[0]; // hand-written loop to find the maximum
for (size_t i = 1; i < n; i++) {
if (xs[i] > best) best = xs[i];
}
printf("%d\n", best);
return 0;
}
Output
7
C++ C++26
C++26
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> xs = {3, 7, 2};
std::cout << *std::max_element(xs.begin(), xs.end()) << "\n";
return 0;
}
Output
7
C# v11.0.100
v11.0.100
Console.WriteLine(new List<int> { 3, 7, 2 }.Max());
Output
7
Go v1.27.4
v1.27.4
package main
import "fmt"
func main() {
fmt.Println(max(3, 7, 2))
}
Output
7
Java v27.0.1
v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(Math.max(3, 7), 2));
}
}
Output
7
JavaScript v26.13.1
v26.13.1
console.log(Math.max(3, 7, 2))
Output
7
PHP v8.6.0
v8.6.0
echo max(3, 7, 2);
Output
7
Python v3.15.2
v3.15.2
print(max(3, 7, 2))
Output
7
Rust v1.99.1
v1.99.1
fn main() {
println!("{}", vec![3, 7, 2].into_iter().max().unwrap());
}
Output
7
TypeScript v26.13.1
v26.13.1
console.log(Math.max(3, 7, 2));
Output
7
Language differences
C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.
C++: takes the maximum with std::max_element, dereferencing the returned iterator.
C#: takes the maximum with LINQ Max() (Enumerable.Max).
Go: has a built-in max since 1.21.
Java: Math.max takes only two arguments (nest them for more).
JavaScript: Math.max.
PHP: built-in max.
Python: built-in max.
Rust: uses the iterator .max() or std::cmp::max.