What are scanf(“%*s”) and scanf(“%*d”) format identifiers?

前端 未结 5 520
太阳男子
太阳男子 2020-11-30 21:26

What is the practical use of the formats \"%*\" in scanf(). If this format exists, there has to be some purpose behind it. The following program gives weird out

5条回答
  •  不思量自难忘°
    2020-11-30 21:53

    The star is a flag character, which says to ignore the text read by the specification. To qoute from the glibc documentation:

    An optional flag character `*', which says to ignore the text read for this specification. When scanf finds a conversion specification that uses this flag, it reads input as directed by the rest of the conversion specification, but it discards this input, does not use a pointer argument, and does not increment the count of successful assignments.

    It is useful in situations when the specification string contains more than one element, eg.: scanf("%d %*s %d", &i, &j) for the "12 test 34" - where i & j are integers and you wish to ignore the rest.

提交回复
热议问题