C Programming - The Role of Spaces in scanf ()

I wrote the following code:

#include <stdio.h>
int main()
{
  int a, b;
  printf("Enter values of a and b\n");
  scanf(" %d%d ", &a, &b); // Note the two spaces before and after %d
  //     ^This^
  printf("a = %d b = %d\n", a, b);
  return 0;
}

The program starts like this:

aps120797@XENON-PC:/mnt/d/Codes/LetUsC$ ./a.out
Enter values of a and b
1
2
3
a = 1 b = 2

My question is why it uses three inputs instead of two (two% d is in scanf ()), and even if it takes three, why does it skip the last?

+1
source share
1 answer

(, , ) , . , ( ) ( getc() scanf(), '3'.

, 2 , . , , 3.

+3

Source: https://habr.com/ru/post/1667923/


All Articles