Home > front end >  why the below code is taking two input on single execution of scanf funcion
why the below code is taking two input on single execution of scanf funcion

Time:11-01

#include<stdio.h>
#include<conio.h>

int main()
{
    int Dividend,divisor;

    printf("Checking Divisiblity of 1st number with 2nd number \n\n") ;

    printf("Enter Number \n") ;
    scanf("%d",&Dividend);

    printf("Enter Divisor = ");
    scanf("%d",&divisor) ;


    if(Dividend % divisor == 0)
    {
        printf("Number %d is divisible by %d",Dividend,divisor) ;
    }
    else
    {
        printf("Number %d is not divisible by %d",Dividend,divisor) ;
    }

    getch();
    return 0;
}

Above is my code that i have written in C ;

on running this program . Only on first execution of scanf function if i give two input space seperated , the second input is going on right variable . and on hitting enter i am getting result . I am not understanding how is this happing .

CodePudding user response:

When space is pressed, scanf doesn't see anything yet. Something happens only after enter is pressed. It then takes everything to the left of the space character and assigns it to the first variable, and everything to the right of the space character and assigns it to the second variable.

If you don't press the spacebar, scanf will interpret everything you type as a single number and will assign it to the first variable.

Instead what you may want to do is use the %c format specifier to read a single character at a time. You can then check if the character is a space character and if it is, you can break out of the loop. Otherwise, you can keep reading characters until you reach a space character.

CodePudding user response:

stdin is line based by default. Your program gets nothing until you press enter. Then your program has the entire line of text available.

Result of this is, that scanf, getchar, fgets, etc calls will not return until you press enter. After enter press, entire line is available and the function starts to process it.


scanf is kinda special, that if you have int parsed_count = scanf("%d%d", &a, &b);, it will read two integers, no matter how many times you press enter, so you can either do

1 2<enter>

Or you can do

<enter>
1<enter>
<enter>
2<enter>

and scanf will still read these two integers (it returns early if there is parse error, which is why you need to check the return value!).


And vice versa, if there is already input available, then scanf may return immediately, so if you have this code

scanf("%d",&Dividend);
printf("Enter Divisor = ");
scanf("%d",&divisor) ;

and enter text

1 2<enter>

Then first scanf will wait for enter press, then consume the first integer, leaving 2<enter> still unread. Then there's print, and then 2nd scanf starts reading, skipping the whitespace and immediately getting 2nd integer. So you see

1 2  <- this is your typing echoed, not print from your program
Enter Divisor =   <- printf printed this
  •  Tags:  
  • c
  • Related