什么是scanf(“%* s")和scanf(“%* d")格式标识符? [英] What are scanf("%*s") and scanf("%*d") format identifiers?

查看:159
本文介绍了什么是scanf(“%* s")和scanf(“%* d")格式标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

scanf()中"%*"格式的实际用途是什么.如果存在这种格式,那么它背后必须有一些目的.以下程序给出了奇怪的输出.

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.

#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;
}

输出:

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:~$

推荐答案

对于printf,*允许您通过额外的参数(例如, printf("%*d", 4, 100);指定字段宽度4.字段宽度4表示如果一个数字打印少于4个字符,则将打印空格字符,直到填充字段宽度为止.如果该数字占用的空间超过了指定的字段宽度,则该数字将按原样打印而不会被截断.

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.

对于scanf,*表示要读取但忽略该字段,例如scanf("%*d %d", &i)输入"12 34"将忽略12并将34读入整数i.

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(“%* s")和scanf(“%* d")格式标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆