You must pass a valid array with at least one member to this function:

#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int
maxValue(int myArray[], size_t size) {
    /* enforce the contract */
    assert(myArray && size);
    size_t i;
    int maxValue = myArray[0];

    for (i = 1; i < size; ++i) {
        if ( myArray[i] > maxValue ) {
            maxValue = myArray[i];
        }
    }
    return maxValue;
}

int
main(void) {
    int i;
    int x[] = {1, 2, 3, 4, 5};
    int *y = malloc(10 * sizeof(*y));

    srand(time(NULL));

    for (i = 0; i < 10; ++i) {
        y[i] = rand();
    }

    printf("Max of x is %d\n", maxValue(x, sizeof(x)/sizeof(x[0])));
    printf("Max of y is %d\n", maxValue(y, 10));

    return 0;
}

By definition, the size of an array cannot be negative. The appropriate variable for array sizes in C is size_t, use it.

Your for loop can start with the second element of the array, because you have already initialized maxValue with the first element.

Answer from Sinan Ünür on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-find-maximum-value-in-an-array-in-c
How to Find Maximum Value in an Array in C? - GeeksforGeeks
July 23, 2025 - We can find the maximal value in an array by taking a variable max and repeatedly comparing it to the elements of the array using loops.
🌐
Reddit
reddit.com › r/homeworkhelp › [c programming: finding maximum element in an array using functions] what's wrong with my code?
r/HomeworkHelp on Reddit: [C Programming: Finding Maximum Element in an Array Using Functions] What's wrong with my code?
April 22, 2021 -

So I made a function to determine the maximum element of an array of integers. But when I try to print the value of the maximum element, it gives me a weird large number 1619117764., and isn't what I'm expecting. What am I doing wrong? Thank you so much in advance.

This is the error message:

C:\Users\USER\Desktop\Discrete Math\Max Value.c|25|warning: passing argument 1 of 'max_value' makes pointer from integer without a cast [-Wint-conversion]|

Here is my code:

#include <stdio.h>

int max_value (int input[])
{
 int size=10, max;
 max=input[0];

 for(int i=1;i<size;i++)
 {
     if(max<input[i])
     {
         max = input[i];
     }
 }

 return max;
}


int main ()
{
    int x, max;
    int input[] = {2, 3, 5, 10, 15, 42, 28, 88, 92};

    x = max_value(max);
    printf("/n The Max is: %d" ,x);

    return 0;
}
Discussions

MIN and MAX in C - Stack Overflow
I've done my best to be thorough ... hope you find it as useful as I do. This technique is commonly used, well-respected by those who know how to use it properly, the "de facto" way of doing things, and fine to use if used properly, but buggy (think: double-evaluation side effect) if you ever pass expressions including variable assignment in to compare: #define MAX(a,b) ((a) ... More on stackoverflow.com
🌐 stackoverflow.com
Function in C to find max element in an array - Stack Overflow
You are participating in a game in which players collect points for various solved puzzles. In the end, the player with the highest score wins. You would like to know how far behind the highest-sco... More on stackoverflow.com
🌐 stackoverflow.com
C functions in finding a max value - Stack Overflow
How to write a C Function that takes three integers as arguments and returns the value of the largest one. int largest(int x,int y,int z) { int val1,val2,val3; int maximum; printf("... More on stackoverflow.com
🌐 stackoverflow.com
February 13, 2016
How to find max and min? (C-Program) - C++ Forum
Then it is supposed to tell "The maximum and minimum" amount of pancakes eaten. I can't seem to figure out a way to find the max and min. ... Easiest way I can think of is to have 3 variables, a current_pancakes (or just what you have, pancakes), a pancakes_max, and a pancakes_min, initialized at ... More on cplusplus.com
🌐 cplusplus.com
🌐
w3resource
w3resource.com › c-programming-exercises › array › c-array-exercise-9.php
C Program: Find the maximum and minimum element in an array - w3resource
September 27, 2025 - The second printf statement asks the user to input n number of elements into the array arr1 using a for loop, and stores each input in the corresponding index of the array arr1[i]. The next for loop then iterates over each element in arr1 and finds the maximum and minimum elements in the array by comparing it to every other element in the array using if statements.
🌐
Phanderson
phanderson.com › C › arraymax.html
C Programming - Finding Maximum in an Array
/* Program ARRAYMAX.C ** ** Illustrates function to find the max all of the elements of an array ** ** Peter H. Anderson, MSU, Feb 21, '97 ** */ #include &ltstdio.h> int max_array(int a[], int num_elements); void print_array(int a[], int num_elements); void main(void) { int a[10] = {1, 2, 0, 0, 4, 5, 6, 9, 9, 17}; int max; printf("\nArray:\n"); print_array(a, 10); max = max_array(a, 10); printf("The max is %d\n", max); } int max_array(int a[], int num_elements) { int i, max=-32000; for (i=0; i&ltnum_elements; i++) { if (a[i]>max) { max=a[i]; } } return(max); } void print_array(int a[], int num_elements) { int i; for(i=0; i&ltnum_elements; i++) { printf("%d ", a[i]); } printf("\n"); }
Top answer
1 of 16
549

Where are MIN and MAX defined in C, if at all?

They aren't.

What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred).

As functions. I wouldn't use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan to deploy your code. Either write your own, use something like standard fmax or fmin, or fix the macro using GCC's typeof (you get typesafety bonus too) in a GCC statement expression:

 #define max(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

Everyone says "oh I know about double evaluation, it's no problem" and a few months down the road, you'll be debugging the silliest problems for hours on end.

Note the use of __typeof__ instead of typeof:

If you are writing a header file that must work when included in ISO C programs, write __typeof__ instead of typeof.

2 of 16
131

It's also provided in the GNU libc (Linux) and FreeBSD versions of sys/param.h, and has the definition provided by dreamlax.


On Debian:

$ uname -sr
Linux 2.6.11

$ cat /etc/debian_version
5.0.2

$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))

$ head -n 2 /usr/include/sys/param.h | grep GNU
This file is part of the GNU C Library.

On FreeBSD:

$ uname -sr
FreeBSD 5.5-STABLE

$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))

The source repositories are here:

  • GNU C Library
  • FreeBSD
🌐
W3schools
w3schools.in › c-programming › examples › find-maximum-element-in-array
C Program to Find Maximum Element in Array - W3schools
... #include <stdio.h> int main() { int array[100], maximum, size, c, location = 1; printf("Enter the number of elements in array\n"); scanf("%d", &size); printf("Enter %d integers\n", size); for (c = 0; c < size; c++) scanf("%d", &array[c]); maximum = array[0]; for (c = 1; c < size; c++) { ...
Find elsewhere
🌐
Learn Java
javatutoring.com › c-program-find-maximum-minimum-element-in-array
C Program To Find Maximum & Minimum Element In Array | C Prorams
August 24, 2026 - Thus, doing the same using multiple methods in C programming is as follows: Read the entered array size and store that value into the variable n. 2) Read the entered elements using scanf and store the entered array elements into the array using ...
🌐
Programiz
programiz.com › c-programming › examples › array-largest-element
C Program to Find Largest Element in an Array
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... #include <stdio.h> int main() { int n; double arr[100]; printf("Enter the number of elements (1 to 100): "); scanf("%d", &n); for (int i = 0; i < n; ++i) { printf("Enter number%d: ", i + 1); scanf("%lf", &arr[i]); } // storing the largest number to arr[0] for (int i = 1; i < n; ++i) { if (arr[0] < arr[i]) { arr[0] = arr[i]; } } printf("Largest element = %.2lf", arr[0]); return 0; }
Top answer
1 of 2
2

"How I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?"

You just need to set max before going into the loop to 0. Then you compare if array[i] is greater than max at each iteration. If it is, max gets assigned by array[i]. The loop condition i < len checks whether there are more elements or not in array:

int max = 0;

for (unsigned int i = 0; i < N; i++) {

    if (array[i] > max) {
       max = array[i];
    }    
}

Another thing is that you have first a fixed size array of 10 elements with

int array[10];

and then later ask the user to input the amount of elements:

scanf("%d", &N);

If the user inputs a number above 11 for N, you accessing memory beyond the bounds of the array in the loops.

In this case, a declaration of array as variable length array after the scanf() call would be more appropriate:

int N, i;

scanf("%d", &N);
int array[N];

Since there will never be more than 10 players: "There are never more than 10 players in the game" - You could also leave array of fixed size with 10 elements and analyze the input of N in a loop and ask the user to reinput a valid number between 0 and 10 if N isn't in this range. You use N for the iterations only but it doesn't influence the array size.

int array[10];

while (1) {

   scanf("%d", &N);

   if ( N >= 0 || N <= 10 ) {
       break;
   }  

   printf("Please reinput a valid number!\n");
}

Side notes:

  • Always check the return value of scanf() for an input error. This happens frequently and to check is a very good practice and makes your program way more safe.

  • VLAs are not portable and only possible to compiler compliant to C99 and above. To maintain portability and to keep safety use an dynamically allocated array instead.

    Related:

    • malloced array VS. variable-length-array

    • Is there any overhead for using variable-length arrays?

    • Is it a good idea to use C99 VLA compared to malloc/free?

2 of 2
1

how I write the code to find the max value of the array elements without comparing the last element to something outside the memory block?

You can code the behind() something like:

void behind(int *arr, int n) {
    int max_value = 0;

    for (int i = 0; i < n; i++)
        if (arr[i] > max_value)
            max_value = arr[i];
    
    printf("Max value: %d\n", max_value);
}

Rather than defining an array, you may declare the array as a pointer variable, that'd be quite helpful. You can then dynamically allocate the memory and then use it like an array thereafter, also note that you're trying to pass a pointer, so it's better take some benefit of pointer declaration.

The full approach to do that:

#include <stdio.h>
#include <stdlib.h> // for dynamic memory allocation

void behind(int *, int);

int main(void) {
    int *array; // declaring as pointer rather than array
    int N, i;

    scanf("%d", &N);

    if (N > 11) { // must be less than 10
        printf("Must be lesser than or equal to 10!\n");
        return -1;
    }

    // dynamically allocating the required memory
    array = (int *)malloc(sizeof(int) * N);

    for (i = 0; i < N; i++)
        scanf("%d", &array[i]);

    // here we go
    behind(array, N);

    for (i = 0; i < N; i++)
        printf("%d\n", array[i]);

    return 0;
}

void behind(int *arr, int n) {
    int max_value = 0;

    for (int i = 0; i < n; i++)
        if (arr[i] > max_value)
            max_value = arr[i];
    
    printf("Max value: %d\n", max_value);
}

An example output is as follows:

5    // ------------ INPUT
12
45
23
1 
05
Max value: 45 // --- OUTPUT
12
45
23
1
5
🌐
Computerscienceai
computerscienceai.com › 2019 › 05 › c-program-find-maximum-number-in-array.html
Computer Science AI: C Program to Find Maximum Number in an Array
February 4, 2021 - Initialize max=arr[0], first element of array. Use for loop, check every time if next element is greater than max , if yes assign that element to max otherwise continue. Print largest element max.
🌐
Codeforwin
codeforwin.org › home › c program to find maximum and minimum using functions
C program to find maximum and minimum using functions - Codeforwin
July 20, 2025 - Hence, the return type of the function must be same as parameters type i.e. int in our case. After combining the above three points, function declaration to find maximum is int max(int num1, int num2);.
🌐
W3Schools
w3schools.com › c › ref_math_fmax.php
C Math fmax() Function
The fmax() function is defined in the <math.h> header file. Tip: Use the fmin() function to return the number with the lowest value. ... Coding fundamentals as bite-sized lessons and challenges.
🌐
Cplusplus
cplusplus.com › forum › beginner › 138425
How to find max and min? (C-Program) - C++ Forum
In English, "if the current person ate more than what was already the known max, the amount that person ate is now the new max". Also, not sure what the point if your person variable is, so maybe I'm misunderstanding what you're trying to do. ... Also remember that C offers size_t which should be used for sizes and lengths instead of plain int. ... @ Ganado: Thank you so much. That was such an easy way of finding them.
🌐
w3resource
w3resource.com › c-programming-exercises › function › c-function-exercise-12.php
C Program: Returns an array of maximum, minimum values
The function finds the minimum and maximum elements of the array arra1 by iterating through all elements of the array and keeping track of the minimum and maximum elements seen so far. The function initializes a static integer array result_mm of size 2 and initializes its first two elements to the first element of the input array arra1[0]. It then enters a for loop that continues from 1 to v-1.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-to-find-the-maximum-and-minimum-element-of-the-array
C Program to Find the Maximum and Minimum Element in the Array - GeeksforGeeks
July 12, 2025 - The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the current element is smaller or larger respectively.
🌐
TutorialsPoint
tutorialspoint.com › article › c-program-to-find-maximum-of-four-integers-by-defining-function
C program to find maximum of four integers by defining function
March 15, 2026 - #include <stdio.h> int max(int ... maximum); return 0; } Numbers: a=10, b=25, c=15, d=8 Maximum number is: 25 · The max() function can be reused for any number comparison · We can use the ternary operator ?: for a more ...
🌐
Programiz
programiz.com › c-programming › examples › largest-number-three
C Program to Find the Largest Number Among Three Numbers
Let's see how they work in greater detail. ... // outer if statement if (n1 >= n2) { // inner if...else if (n1 >= n3) printf("%.2lf is the largest number.", n1); else printf("%.2lf is the largest number.", n3); } Here, we are checking if n1 is greater than or equal to n2.