C语言 什么是 scanf("%*s") 和 scanf("%*d") 格式标识符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2155518/
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
What are scanf("%*s") and scanf("%*d") format identifiers?
提问by manav m-n
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 output.
"%*"scanf() 中格式的实际用途是什么?如果这种格式存在,那么它背后一定有某种目的。下面的程序给出了奇怪的输出。
#include<stdio.h>
int main()
{
int i;
char str[1024];
printf("Enter text: ");
scanf("%*s", &str);
printf("%s\n", str);
printf("Enter interger: ");
scanf("%*d", &i);
printf("%d\n", i);
return 0;
}
Output:
输出:
manav@workstation:~$ gcc -Wall -pedantic d.c
d.c: In function ‘main':
d.c:8: warning: too many arguments for format
d.c:12: warning: too many arguments for format
manav@manav-workstation:~$ ./a.out
Enter text: manav
D
Enter interger: 12345
372
manav@workstation:~$
回答by H?vard S
For printf, the * allows you to specify the minimum field width through an extra parameter, e.g. printf("%*d", 4, 100);specifies a field width of 4. A field width of 4 means that if a number takes less than 4 characters to print, space characters are printed until the field width is filled. If the number takes up more space than the specified field width, the number is printed as-is with no truncation.
对于 printf,* 允许您通过一个额外的参数指定最小字段宽度,例如printf("%*d", 4, 100);指定字段宽度为 4。字段宽度为 4 意味着如果打印数字少于 4 个字符,则打印空格字符,直到字段宽度已填充。如果数字占用的空间大于指定的字段宽度,则数字按原样打印,不会被截断。
For scanf, the * indicates that the field is to be read but ignored, so that e.g. scanf("%*d %d", &i)for the input "12 34" will ignore 12 and read 34 into the integer i.
对于scanf,* 表示该字段将被读取但被忽略,因此例如scanf("%*d %d", &i)对于输入“12 34”将忽略 12 并将 34 读入整数 i。
回答by Zoltán Sz?cs
The star is a flag character, which says to ignore the text read by the specification. To qoute from the glibc documentation:
星号是一个标志字符,表示忽略规范读取的文本。从 glibc 文档中引用:
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.
一个可选的标志字符“*”,表示忽略为该规范读取的文本。当 scanf 找到使用此标志的转换规范时,它会按照转换规范其余部分的指示读取输入,但它会丢弃此输入,不使用指针参数,也不增加成功赋值的计数。
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& jare integers and you wish to ignore the rest.
它在规范字符串包含多个元素的情况下很有用,例如:scanf("%d %*s %d", &i, &j)对于"12 test 34"- 其中i和j是整数而您希望忽略其余元素
。
回答by Jon Cage
回答by interjay
The *is used to skip an input without putting it in any variable. So scanf("%*d %d", &i);would read two integers and put the second one in i.
所述*用于跳过的输入而不把它在任何变量。所以scanf("%*d %d", &i);会读取两个整数并将第二个放入i.
The value that was output in your code is just the value that was in the uninitialized ivariable - the scanf call didn't change it.
代码中输出的值只是未初始化i变量中的值 - scanf 调用没有更改它。
回答by Raunak Chowdhury
In scanf("%*d",&a)*skips the input. In order to read the inputs one has to use an extra "%d"in scanf. For example:
在 scanf("%*d",&a)*跳过输入。为了读取输入,必须使用额外的 "%d"in scanf。例如:
int a=1,b=2,c=3;
scanf("%d %*d %d",&a,&b,&c); //input is given as: 10 20 30
O/p:
开/关:
a=10 b=30 and c=3; // 20 is skipped
If you use another %di.e: scanf("%d %*d %d %d",&a,&b,&c); //input is given as: 10 20 30 40then a=10 b=30 c=40.
如果您使用另一个%die: scanf("%d %*d %d %d",&a,&b,&c); //input is given as: 10 20 30 40那么 a=10 b=30 c=40。
If you use ","in scanf then no value will be taken after %*di.e;
scanf("%d %*d,%d" &a,&b,&c)// 10 20 30O/p:
a=10 b=2 c=3 will be the output.
如果您","在 scanf 中使用,则在%*die之后将不取任何值;
scanf("%d %*d,%d" &a,&b,&c)// 10 20 30O/p: a=10 b=2 c=3 将是输出。

