-3

The code is:

int main(){
   int i=1;
   int j=2;
   char c='a';
   int x=-1;
   x=scanf("%d%d%c", &i, &j,&c);
   printf("%d",x);
}

when you enter 5,6$, this output is 1. why?

Simson
  • 3,373
  • 2
  • 24
  • 38

1 Answers1

3

The scanf function returns because it read one field successfully. You asked it to read two integers followed by a character. It read one integer (the 5) but then choked on the comma. So one successful field means a return value of one.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278