-3
#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 .

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • Two of three tags are wrong. – 273K Nov 01 '22 at 07:52
  • Please don't tag irrelevant languages, this is neither c++ or Java – Alan Birtles Nov 01 '22 at 07:53
  • `scanf` is like that. Your program asked for two numbers, you typed two numbers, the two `scanf` calls each got their number. `scanf` doesn't know, or care, whether you hit Return between the two numbers — any whitespace will do. So `scanf` is "working as designed" here. – Steve Summit Nov 02 '22 at 19:15
  • If you want different behavior, you should [use something else](https://stackoverflow.com/questions/58403537). – Steve Summit Nov 02 '22 at 19:20
  • To see just how strange `scanf` can be, try running your program, and when it says "Enter Number", hit Enter five times before you start typing the number. (And then maybe hit Enter five more times before you type the divisor.) – Steve Summit Nov 02 '22 at 19:56

2 Answers2

1

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.

Igor
  • 474
  • 2
  • 10
  • @hyde Ok I will edit my answer – Igor Nov 01 '22 at 08:00
  • No I want to take a multi digit Number but at the same time I also want that it will only take one input despite of giving multi space separated input on single enter hit. Could you tell me how can I achieve this one – Prashant Kumar Gupta Nov 04 '22 at 04:46
0

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

If you want to take only one input per enter press, you can simply read characters until newline, because scanf leaves them there. Example loop to read until newline, or exit program at end of file/error:

while(true) {
    int ch = getchar();
    if (ch == EOF) exit(1);
    if (ch == '\n') break;
}
hyde
  • 60,639
  • 21
  • 115
  • 176
  • sir can you just tell me how can i fix it that even after giving two input it won't take – Prashant Kumar Gupta Nov 01 '22 at 13:10
  • @PrashantKumarGupta You mean, accept only one input number per enter press? – hyde Nov 01 '22 at 14:17
  • Yes on each enter key press it take only one input8 even after giving it multiple space separated entry – Prashant Kumar Gupta Nov 02 '22 at 16:14
  • @PrashantKumarGupta Simply read until end of line after calling `scanf`. See edit. – hyde Nov 02 '22 at 19:10
  • @PrashantKumarGupta My advice to you is: either don't worry about solving this "problem" (and I'm still not sure what the problem actually is), or if you must solve it, solve it by [using something other than `scanf`](https://stackoverflow.com/questions/58403537). `scanf` is not inherently line-based, and trying to force it to operate rigidly on lines is more trouble than it's worth. – Steve Summit Nov 02 '22 at 19:53