(I'm going to ignore handling input, its just a distraction.)
The easy way is to sort it.
#include <stdlib.h>
#include <stdio.h>
int cmp_int( const void *a, const void *b ) {
return *(int*)a - *(int*)b;
}
int main() {
int a[] = { 1, 5, 3, 2, 0, 5, 7, 6 };
const int n = sizeof(a) / sizeof(a[0]);
qsort(a, n, sizeof(a[0]), cmp_int);
printf("%d %d\n", a[n-1], a[n-2]);
}
But that isn't the most efficient because it's O(n log n), meaning as the array gets bigger the number of comparisons gets bigger faster. Not too fast, slower than exponential, but we can do better.
We can do it in O(n) or "linear time" meaning as the array gets bigger the number of comparisons grows at the same rate.
Loop through the array tracking the max, that's the usual way to find the max. When you find a new max, the old max becomes the 2nd highest number.
Instead of having a second loop to find the 2nd highest number, throw in a special case for running into the 2nd highest number.
#include <stdio.h>
#include <limits.h>
int main() {
int a[] = { 1, 5, 3, 2, 0, 5, 7, 6 };
// This trick to get the size of an array only works on stack allocated arrays.
const int n = sizeof(a) / sizeof(a[0]);
// Initialize them to the smallest possible integer.
// This avoids having to special case the first elements.
int max = INT_MIN;
int second_max = INT_MIN;
for( int i = 0; i < n; i++ ) {
// Is it the max?
if( a[i] > max ) {
// Make the old max the new 2nd max.
second_max = max;
// This is the new max.
max = a[i];
}
// It's not the max, is it the 2nd max?
else if( a[i] > second_max ) {
second_max = a[i];
}
}
printf("max: %d, second_max: %d\n", max, second_max);
}
There might be a more elegant way to do it, but that will do, at most, 2n comparisons. At best it will do n.
Note that there's an open question of what to do with { 1, 2, 3, 3 }. Should that return 3, 3 or 2, 3? I'll leave that to you to decide and adjust accordingly.