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 - // C program to find maximum value in an array #include <stdio.h> int main() { // Initialize an array int arr[] = { 23, 12, 45, 20, 90, 89, 95, 32, 65, 19 }; // Find the size of the array int n = sizeof(arr) / sizeof(arr[0]); // Intialize the ...
🌐
Programiz
programiz.com › c-programming › examples › array-largest-element
C Program to Find Largest Element in an Array
... #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 ...
Discussions

C code to find max and min values: unexpected results
After the first loop, the value of i is 5, and the following is undefined, you are reading past the last element of the array numbers: max = numbers[i]; min = numbers[i]; To understand why, note that the loop for (i = 0; i < 5; i++) { /* loop body */ } is exactly equivalent to i = 0; while (i < 5) { /* loop body */ i++; } The while loop stops when the test is no longer true, that is, at i=5. Therefore, just after the loop, i=5. Then, you are reading a number in memory, just after the array. It's invalid, but the program simply reads the value there. It happens to be a large negative number. If you initialize min and max at the beginning of the function, but leave those two lines, the initial value is just overwritten with this invalid value. The simplest to fix this would be to initialize with numbers[0]. And the next for loop may start at i=1, since the 0 case is already taken into account. You have another problem: since average is an int, the exact average will be truncated. You may declare average as a double instead, and change the printf format specifier accordingly. More on reddit.com
🌐 r/cprogramming
13
7
January 12, 2025
C programming problem to find largest number
I’m trying to make a function which help to find largest number. But it’s not work properly.If i input gradually 2 5 6 8 then output is 8;That’s mean Right! But if i input 2 8 6 5 then output is 5;That’s mean function is not work properly. where i did mistake ☹ Can you help me please ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
October 14, 2019
[C Programming: Finding Maximum Element in an Array Using Functions] What's wrong with my code?
Off-topic Comments Section All top-level comments have to be an answer or follow-up question to the post. All sidetracks should be directed to this comment thread as per Rule 9. OP and Valued/Notable Contributors can close this post by using /lock command I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/HomeworkHelp
15
4
April 22, 2021
c - How can I write a program to find maximum value of integer variable - Stack Overflow
In C, how can I write a program to find the maximum value of an integer variable? As far as I know the maximum value of an integer variable is 2147483647 but how can I represent this in a C program ? More on stackoverflow.com
🌐 stackoverflow.com
March 18, 2010
🌐
Quora
quora.com › How-do-I-find-the-max-number-from-28-74-65-45-67-without-any-array-library-function-in-C
How to find the max number from {28, 74, 65, 45, 67} without any array library function in C - Quora
Answer (1 of 2): You can use a loop to iterate through the numbers in the set, and keep track of the largest number seen so far. Here is an example of how you can find the maximum number in the set {28, 74, 65, 45, 67} using a for loop: ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-to-find-the-largest-number-among-three-numbers
C program to Find the Largest Number Among Three Numbers - GeeksforGeeks
In this method, we assume one of the numbers as maximum and assign it to a temporary variable. We then compare this temporary variable with the other two numbers one by one and if the max is smaller than the compared number, we assign the compared ...
Published   May 1, 2025
🌐
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
September 14, 2023 - #include <stdio.h> int max(int x, int y){ if(x > y){ return x; }else{ return y; } } int main(){ int a = 5, b = 8, c = 2, d = 3; int left_max = max(a, b); int right_max = max(c, d); int final_max = max(left_max, right_max); printf("Maximum number is: %d", final_max); }
🌐
Reddit
reddit.com › r/cprogramming › c code to find max and min values: unexpected results
r/cprogramming on Reddit: C code to find max and min values: unexpected results
January 12, 2025 -

Hi everyone,

I'm trying to find the maximum and minimum values in a C array, but I'm running into a problem. My code calculates the maximum value correctly, but the minimum value is always a very large negative number, even when all the values in the array are positive.

I've tried initializing the min variable to a large positive number, but it doesn't seem to help.

Here's my code:

#include <stdio.h>

int main(void)
{
    int i, sum = 0;
    int numbers [5];
    int min, max, average;
    
    printf("enter 5 numbers:\n");
    
    for (i = 0; i < 5; i++)
    {
        scanf("%d", &numbers[i]);
        sum += numbers[i];
    }
    
    max = numbers[i];
    min = numbers[i];
    
    for (i = 0; i < 5 ; i++)
    {
        if (numbers[i] > max)
        {
            max = numbers[i];
        }
        if (numbers[i] < min)
        {
            min = numbers[i];
        }
        
    }
    
    average = (double)sum/5;
    
    printf("Average is %d and sum is %d\n", average, sum);
    printf("Max number is %d and the min number is %d\n", max, min);
    
}

Can anyone help me figure out what's going wrong?

Thanks!

Find elsewhere
🌐
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.
🌐
NAO IT Systems LLC.
digibeatrix.com › home › how to use functions › finding the maximum value in c: functions, macros, arrays
Finding the Maximum Value in C: Functions, Macros, Arrays - C Programming for System Development
September 18, 2025 - Here, we introduce two implementation ... among multiple numbers stored in an array, the basic approach is to use a loop to compare each element one by one....
🌐
Onlinegdb
learn.onlinegdb.com › c_program_find_maximum_numbers_from_entered_numbers
C Program to find maximum numbers from entered numbers | Learn Programming step by step
Problem Statement: Write a C Program to find maximum numbers from entered numbers. Required Knowledge: C Input/Output, C Variables, C Datatypes, C for..loop
🌐
w3resource
w3resource.com › c-programming-exercises › pointer › c-pointer-exercise-6.php
C Program: Find the maximum number between two numbers - w3resource
November 1, 2025 - #include <stdio.h> #include <stdlib.h> int main() { int fno, sno, *ptr1 = &fno, *ptr2 = &sno; printf("\n\n Pointer : Find the maximum number between two numbers :\n"); printf("------------------------------------------------------------\n"); printf(" Input the first number : "); scanf("%d", ptr1); // Read the first number from the user and store it using ptr1 printf(" Input the second number : "); scanf("%d", ptr2); // Read the second number from the user and store it using ptr2 // Compare the values pointed by ptr1 and ptr2 to find the maximum number if (*ptr1 > *ptr2) { printf("\n\n %d is the maximum number.\n\n", *ptr1); // Print the maximum number } else { printf("\n\n %d is the maximum number.\n\n", *ptr2); // Print the maximum number } return 0; }
🌐
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.
🌐
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);.
🌐
LabEx
labex.io › tutorials › c-find-the-largest-number-among-n-numbers-123252
C Programming | Find the Largest Number | LabEx
Learn how to write a C program to find the largest number among n user input numbers using a simple for loop and variable comparison.
🌐
Codeforwin
codeforwin.org › home › c program to find maximum between two numbers
C program to find maximum between two numbers - Codeforwin
July 20, 2025 - We can write expression to find maximum between num1 and num2 as num1 > num2. The expression num1 > num2 evaluate 1 if num1 is greater than num2, otherwise evaluates 0. After finding maximum, we need to execute some action based on the maximum ...
🌐
Quora
quora.com › How-do-I-find-the-largest-element-of-an-array-in-C
How to find the largest element of an array in C - Quora
Basically when you have to find the largest/smallest number you basically have to find the largest/smallest out of these thus you’ ll have to make comparisons with all. Hoewever, comparisons of each term with each term may not be feasible. Thus the idea is to store the position(say int pos;) and the value of the max/min element in some variable(let’s say int max), as soon as you find one, and then see if there’s another no which is bigger/smaller than the number you’d stored.
🌐
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;
}
🌐
Medium
sbalgobin94.medium.com › how-to-find-the-maximum-value-in-an-unsorted-array-c-30aa27219dfa
How to Find the Maximum Value in an Unsorted Array: C | by Samantha Balgobin | Medium
November 10, 2020 - int nums[] = {10, 2, 12, 4, 8};// Create a variable called largest and set it equal to the first element of the arrayint maximum = nums[0]; We then find the number of elements in the array and store that quantity in a variable to use for our loop.