Biggest value of variable is always 0?

Oct 14, 2020 at 8:18am
I am re-/learning and want to get better in C and wrote a short program which purpose is to give the biggest value of 10 entered values. The thing is that I always get 0 as result. The values are correctly stored but the if() is never executed?

Thank you.

Marco.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#define DEBUG_MK printf("%d", biggest_value)

int main(int argc, char **argv)
{
   int input_values[10] = {0};
   double biggest_value = 0;

   printf("\n");
   for (int i = 0; i < 10; ++i)
   {
      printf("Enter value %i: ", (i + 1));
      scanf("%i", &input_values[i]);
   }

   biggest_value = input_values[0];
   DEBUG_MK;

   for (int x = 1; x < 10; ++x)
   {
      if (input_values[x] > biggest_value)
         biggest_value = input_values[x];
   }

   printf("\nBiggest value you entered is: %d\n", biggest_value);

   printf("Your entered values:\n");
   for (int i = 0; i < 10; printf("%d\t", input_values[i]), ++i)
      ;

   return 0;
}
Oct 14, 2020 at 8:27am
int biggest_value = 0;
Oct 14, 2020 at 8:29am
The entered values are of type int, but biggest_value is of type double and is displayed as a type int. Either change biggest_value to type int or output biggest_value as %f rather than %d.
Oct 14, 2020 at 8:31am
That was fast. Thank you lastchance.
I thought there is an implicit conversion by the compiler?
Oct 14, 2020 at 8:34am
I thought there is an implicit conversion by the compiler?

Yes there is. But that's not the problem.
You tried to output a double with a %d format specifier. That's the problem. Strangely, 'd' doesn't stand for double here!


BTW, using printf and scanf without flushing streams doesn't work very well on either my PC or the cpp shell.
Last edited on Oct 14, 2020 at 8:37am
Oct 14, 2020 at 12:30pm
Thanks all. Now I understand. Little thing with big impact. Didn't know this.
Topic archived. No new replies allowed.