What is the effect of trailing white space in a scanf() format string?

后端 未结 4 1729
小蘑菇
小蘑菇 2020-11-21 07:01

What is difference between scanf(\"%d\") and scanf(\"%d \") in this code, where the difference is the trailing blank in the format string?

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 07:34

    A white-space character (space, newline, horizontal and vertical tab) in a format string matches any number of white-space characters in the input.
    In your first case

      scanf("%d  ",&j);
    

    when it encounters the white-space char (WSC) ' ' then it will eat all the white spaces input by user including \n on pressing Enter and it will expect to enter a non-WSC . In this case your program will terminate by pressing Ctrl + Z.

提交回复
热议问题