r/C_Homework • • Oct 12 '20

what is wrong with my code

#include <stdio.h>
int main()
{
	float a, b, c;
	printf("type 3 number\n"); 
	scanf("%f %f %f\n",&a,&b,&c);
	if (a>b&&a>c)
	{
		printf("%f\n",a);
	}
	if (b>a&&b>c)
	{
		printf("%f\n",b); 
	}
	if (c>a&&c>b) 
	{
		printf("%f\n",c);
	}
return 0;
 } 

output is weird. I have to input 4 numbers and the output is the largest one among the first 3 one

type 3 number
34 665 677 9999
677.000000

--------------------------------
Process exited after 11.85 seconds with return value 0
2 Upvotes

7 comments sorted by

1

u/jedwardsol Oct 12 '20

What's weird about it?

The largest is 677 and that's what it printed.

1

u/Ryeanney Oct 12 '20

I was expected to input 3 numbers and it outputs the largest. But actually I have to input 4 numbers to get the output. (When I have typed 3 numbers and pressed enter nothing happened)

2

u/jedwardsol Oct 12 '20

Take out the \n from the scanf format string.

1

u/Ryeanney Oct 12 '20

It works.Thank you so much. But I just wonder why. \n just means to move my cursor to the next line.

2

u/jedwardsol Oct 12 '20

It means that when used in printf

scanf is reading input - each character in the format string has to be matched with what you type.

1

u/Ryeanney Oct 12 '20

I see. Thank you again for explaining it for me.

1

u/Ryeanney Oct 12 '20

It works.Thank you so much. But I just wonder why. \n just means to move my cursor to the next line.