sscanf behaviour / return value

前端 未结 3 1932
眼角桃花
眼角桃花 2021-01-11 23:07

I\'m a novice learning C and trying to understand the following code from an online lecture. It scans a string for an integer; if characters are encountered, the sscanf fail

3条回答
  •  爱一瞬间的悲伤
    2021-01-11 23:44

    Your sscanf(string, " %d %c") will return EOF, 0,1 or 2:

    2: If your input matches the following
    [Optional spaces][decimal digits*][Optional spaces][any character][extra ignored]

    1: If your input failed above but matched the following
    [Optional spaces][decimal digits*][Optional spaces][no more data]

    [Correction]
    0: If your input, after white-space and an optional sign, did not find a digit: examples: "z" or "-".

    EOF: If input was empty "" or only white-space.

    • The decimal digits may be preceded by a sign character + or -.

提交回复
热议问题