0

I have written the following code:

#include <stdio.h>

/* max_number.c: outputs the largest number of five numbers entered */

void main(void) {
  int i, num, max;

  for (i = 1; i <= 5; i++) {
    printf("Enter a number: ");
    scanf("%d", &num);

    if (num >= max)
      max = num;
  }
  printf("The maximum number is %d\n", max);
}

When I run the program with any type of data I continually get "The maximum number is 14". Can someone please point me in the direction of what I am doing wrong? Thank you!

1 Answer 1

4

Variable max is not initialized.

Try

int i, num, max = INT_MIN;
Sign up to request clarification or add additional context in comments.

4 Comments

Exactly. The 14 must come from somewhere deterministic, but it is the uninitialized value.
Interesting. Why would not initializing max to a value cause the program to give me that kind of output? Thank you!
@user3727648 Uninitialized variable still has some value. I would not be surprised if max just happens to have value 14 in your case. And if all you input is less or equal to 14, then it is the final result.
Thank you all for your help and explanation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.