0

Why it is asking for 4 input instead of 3 ? I have used only 3 scanf in my program. This is a program about finding smallest and largest number among 3 variables. Please Help...

#include <stdio.h>
int main()

{
    int first, second, third;
    printf("Enter 3 integers for compare:");
    scanf("%d\n",&first);
    scanf("%d\n",&second);
    scanf("%d\n",&third);
    if((first>second) && (first>third))
        printf("First number is the largest\n");
    else if((second>first) && (second>third))
        printf("Second number is the largest\n");
    else if((third>second) && (third>first))
        printf("\nThird number is the largest\n");

    if((first<second) && (first<third))
        printf("First number is smallest\n");
    else if((second<first) && (second<third))
        printf("Second number is smallest\n");
    else if((third<second) && (third<first))
        printf("Third number is smallest\n");

    return 0;
}
Maruf Sarkar
  • 1
  • 1
  • 1

1 Answers1

1

Lets take your last call to scanf:

scanf("%d\n",&third);

The "\n" at the end of the format string means the scanf function will read and discard any white-space after the input, but it can't know when the white-space ends until there is something which is not a white-space character. That means you need to enter some extra input for scanf to be satisfied.

So the simple solution is to just don't have the newlines in your scanf format strings.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621