什么是scanf函数(QUOT;%* S")和scanf(QUOT;%* D")格式标识符? [英] what is scanf("%*s") and scanf("%*d") format identifiers?

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

问题描述

什么是实际使用的格式%*在scanf()的。如果这个格式存在,必须有一些背后的目的。下面的程序给出了奇怪的输出。

 #包括LT&;&stdio.h中GT;
诠释的main()
{
        INT I;
        焦炭海峡[1024];        的printf(请输入文字:);
        scanf函数(%* S,&安培; STR);
        的printf(%S \\ n,STR);        的printf(请输入整数:);
        scanf函数(%* D,&安培; I)
        的printf(%d个\\ N,I);
        返回0;
}

输出:

  manav @工作站:〜$ GCC -Wall -pedantic特区
特区:在函数'主':
特区:8:警告:对格式的参数太多
直流转换:12:警告:对格式的参数太多
manav @ manav工作站:〜$ ./a.out
输入文本:manav
ð
输入整数:12345
372
manav @工作站:〜$


解决方案

有关的printf的*允许您通过一个额外的参数,即的printf(%* D,4指定最小字段宽度,100); 指定的4场宽度

有关 scanf函数,*表示该字段是要读,但忽略了,这样即 scanf函数(%* D%D ,&安培; I)对于输入12 34会忽略12和34读入整数i

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

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

解决方案

For printf, the * allows you to specify minimum field width through an extra parameter, i.e. printf("%*d", 4, 100); specifies a field width of 4.

For scanf, the * indicates that the field is to be read but ignored, so that i.e. scanf("%*d %d", &i) for the input "12 34" will ignore 12 and read 34 into the integer i.

这篇关于什么是scanf函数(QUOT;%* S&QUOT;)和scanf(QUOT;%* D&QUOT;)格式标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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