2
#include<stdio.h>
int main() {
    int a,b;
    printf("Enter values of a and b\n");
    scanf(" %d%d ",&a,&b);
    printf("a=%d b=%d", a, b);
    return 0 ;
}

Here if I use scanf(), as in my code then the compiler expects the user to enter three values, I am unable to understand this , when i use scanf() without any white space then it asks for only two inputs as expected , so i am confused whats the difference between these two , plz explain ...

Gal Silberman
  • 3,756
  • 4
  • 31
  • 58
LocalHost
  • 910
  • 3
  • 8
  • 24
  • Do you mean the whitespace after the second `%d` or before the first one? Do you want to ignore the first or the third value? – mch Dec 20 '17 at 10:16
  • i am asking for both , before first %d and after second %d , i just want to know that why the compiler asks for three inputs when i use a white space in scanf like in my code – LocalHost Dec 20 '17 at 10:19
  • The readable version of ignoring an input is to use `%*d`. – mch Dec 20 '17 at 10:25
  • what should i do if i want to enter only two integers rather than three , for the same code with white space as mentioned in the question ???? – LocalHost Dec 20 '17 at 10:27
  • 1
    `scanf("%d%d", &a, &b);` will read 2 integers and nothing more. – mch Dec 20 '17 at 10:28
  • will scanf(" %d%d ",&a,&b); always read 3 integers ??? – LocalHost Dec 20 '17 at 10:29
  • #include int main() { int a,b; printf("Enter values of a and b\n"); scanf("%d\n%d",&a,&b); printf("a=%d b=%d", a, b); return 0 ; } – Prabhat Kasera Dec 20 '17 at 10:35
  • 1
    Note that the leading whitespace in the format string is not needed since the `%d` directive _automatically_ ignores initial whitespace characters, and the trailing whitespace in the format string is a mistake which causes problems for interactive input. – ad absurdum Dec 20 '17 at 10:53
  • @user3121023 can u please explain it more clearly – LocalHost Dec 20 '17 at 11:03

2 Answers2

1

If you give a space after your numbers, scanf must determine the end of the whitespaces to be matched:

scanf("%d ", &a);

Here the space means read and discard all whitespaces. A non-whitespace character (or EOF) must appear for scanf to make it clear what all is so they can be properly read and discarded.

Consider this input stream (dots are character indicators):

   1   2
........

If you call scanf("%d"), then after calling, the leftover stream is

   2
....

... where the whitespaces will be discarded at next read. Note the leading spaces are automatically discarded when reading the number.

If you call scanf("%d ") instead, the leftover stream is

2
.

You see the whitespaces are gone immediately.

iBug
  • 35,554
  • 7
  • 89
  • 134
0

Take a look at this.

You can use a space between the %d's. That will require from the user to input something like: 12 32 (with the spaces).

If you don't want that, you should use a loop with the scanf.

Good luck.

Gal Silberman
  • 3,756
  • 4
  • 31
  • 58
  • i am not asking about space between the two %d's , i am asking about the white space before and after my first and second %d in the scanf statement, how do they modify my program ? – LocalHost Dec 20 '17 at 10:22
  • 1
    A space between the `%d`s does not change anything. A whitespace means "ignore all whitespaces", but a `%d` also ignores all whitespaces. – mch Dec 20 '17 at 10:23