C语言 scanf() 跳过变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7607550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:46:01  来源:igfitidea点击:

scanf() skip variable

cscanf

提问by ross

In C, using scanf()with the parameters, scanf("%d %*d", &a, &b)acts differently. It enters value for just one variable not two!

在 C 中,scanf()与参数一起使用,scanf("%d %*d", &a, &b)作用不同。它只为一个变量而不是两个变量输入值!

Please explain this!

请解释一下!

scanf("%d %*d", &a, &b);

回答by jpalecek

The *basically means the specifier is ignored (integer is read, but not assigned).

所述*基本上意味着说明符被忽略(整数被读取,但是未分配)。

Quotation from man scanf:

引自man scanf

 *        Suppresses assignment.  The conversion that follows occurs as
          usual, but no pointer is used; the result of the conversion is
          simply discarded.
 *        Suppresses assignment.  The conversion that follows occurs as
          usual, but no pointer is used; the result of the conversion is
          simply discarded.

回答by Tomek Szpakowicz

Asterisk (*) means that the value for format will be read but won't be written into variable. scanfdoesn't expect variable pointer in its parameter list for this value. You should write:

星号 (*) 表示将读取 format 的值,但不会将其写入变量。scanf不希望在其参数列表中为该值提供变量指针。你应该写:

scanf("%d %*d",&a);

回答by reader_1000

http://en.wikipedia.org/wiki/Scanf#Format_string_specifications

http://en.wikipedia.org/wiki/Scanf#Format_string_specifications

An optional asterisk (*) right after the percent symbol denotes that the datum read by this format specifier is not to be stored in a variable.

百分比符号后面的可选星号 (*) 表示该格式说明符读取的数据不存储在变量中。