C Maximum Value of an Array Program

Beginner
5 min read
Updated: Oct 2026
2 programs
Live preview

What Is the Maximum Value of an Array?

The maximum is the largest element in the array. For an unsorted array, scan once from left to right and keep a running best.

Remember
arr = [12, 45, 7, 42, 19]
max = 45

Idea:
  max = arr[0]
  for each later element x:
      if x > max → max = x

Works for negatives too:
  [-8, -1, -15] → max = -1
ArrayMaximum
[12, 45, 7, 42, 19]45
[-8, -1, -15]-1
[5]5
[]undefined — handle empty

How to Find the Maximum in C

Four clear steps — one linear pass is enough.

  1. Reject an empty array (n <= 0 — no maximum exists).
  2. Set max = arr[0] as the first candidate.
  3. Loop from index 1 to n - 1.
  4. Whenever arr[i] > max, update max (optionally track the index too).
MethodIdeaTimeBest for
Linear scanRunning maximum with >O(n)Interviews, unsorted arrays
Track indexSame scan, also store the positionO(n)When you need where the max is

Pseudocode

Pseudocode
function FindMax(arr, n):
    if n <= 0:
        error "empty array"

    max = arr[0]
    for i from 1 to n - 1:
        if arr[i] > max:
            max = arr[i]

    return max

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Startmax = arr[0]
Updateif arr[i] > max
EmptyCheck n > 0 before reading arr[0]
NegativesStill use > — do not seed with 0
AvoidSorting just to get the max
Classic check[12, 45, 7, 42, 19] → 45

Live Preview

Pick a sample array to see the running maximum after each step.

Tap a chip to run the linear scan trace.

Result
arr = [12, 45, 7, 42, 19]
start max = 12
45 > 12 → max = 45
7  ≤ 45
42 ≤ 45
19 ≤ 45
Maximum = 45

Worked Walkthrough — [12, 45, 7, 42, 19]

Start at the first element and update whenever a larger value appears.

Calculation
max = 12

i=1 → 45 > 12 → max = 45
i=2 → 7  ≤ 45 → no change
i=3 → 42 ≤ 45 → no change
i=4 → 19 ≤ 45 → no change

Maximum = 45

C Programs

Start with a simple running maximum. Then track the index when you also need the position.

Example 1 — Linear Scan

Keep a running maximum and update it whenever a larger element appears.

C
#include <stdio.h>

int find_max(int arr[], int n)
{
    int i, max;

    if (n <= 0)
    {
        printf("Array must not be empty.\\n");
        return 0;
    }

    max = arr[0];
    for (i = 1; i < n; i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
        }
    }
    return max;
}

int main(void)
{
    int numbers[] = { 12, 45, 7, 42, 19 };
    int n = (int)(sizeof numbers / sizeof numbers[0]);

    printf("Maximum = %d\\n", find_max(numbers, n));
    return 0;
}

How It Works

  1. Reject empty arrays (n <= 0) before reading arr[0].
  2. Seed max with the first element.
  3. Loop from index 1 so you never compare the seed against itself needlessly.
  4. Update when arr[i] > max.
  5. Return the final running maximum after one pass.

Example 2 — Track Index

Same scan, but also remember the first index where the maximum appears.

C
#include <stdio.h>

int find_max_index(int arr[], int n)
{
    int i, max_index;

    if (n <= 0)
    {
        printf("Array must not be empty.\\n");
        return -1;
    }

    max_index = 0;
    for (i = 1; i < n; i++)
    {
        if (arr[i] > arr[max_index])
        {
            max_index = i;
        }
    }
    return max_index;
}

int main(void)
{
    int numbers[] = { 12, 45, 7, 42, 19 };
    int n = (int)(sizeof numbers / sizeof numbers[0]);
    int index = find_max_index(numbers, n);

    if (index >= 0)
    {
        printf("Max = %d at index %d\\n", numbers[index], index);
    }
    return 0;
}

How It Works

  1. Start with max_index = 0 (first element is the candidate).
  2. Compare each later element to arr[max_index].
  3. When a larger value appears, store that new index.
  4. Ties keep the earlier index because the test uses strict >.
  5. Read the value with numbers[index] after the scan.

Edge Cases & Pitfalls

Check these before calling the solution done.

Empty

No maximum

Validate n > 0 before using arr[0].

Negatives

Still works

Do not seed with 0 — that fails for all-negative arrays.

Duplicates

Same answer

Multiple copies of the max still return that value.

Sorting

Overkill

One scan is enough; sorting is usually slower for this goal.

Time and Space Complexity

ProgramTimeExtra space
Linear scanO(n)O(1)
Track indexO(n)O(1)

You need about n − 1 comparisons in the worst case. One pass is optimal for an unsorted array.

Key Takeaways

  • Pattern: seed with arr[0], then scan with >.
  • Empty: validate n > 0 before reading the first element.
  • Index: same loop can track where the max sits.
  • Test: [12, 45, 7, 42, 19] → 45.

One line: one pass, keep the larger value — that is the maximum.

Frequently Asked Questions

The first element is a valid candidate. Then every later element is compared against that running maximum.
An empty array has no maximum. Check that n > 0 before reading arr[0], then return early or print an error.
Yes. The maximum is still the greatest value, even if it is negative (for example −1 is greater than −8).
No. Sorting is usually slower than one linear scan when you only need the maximum.
One pass over n elements: O(n) time and O(1) extra space.

Next: Minimum of an Array

Find the smallest element with the same linear-scan pattern.

Minimum of array tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful