scanf() 中的 %*c - 这是什么意思? [英] %*c in scanf() - what does it mean?

查看:73
本文介绍了scanf() 中的 %*c - 这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 Turbo C 中运行此程序,但无法破译输出.这个 %*c 是什么意思?任何帮助将不胜感激.

I tried to run this program in Turbo C but couldn't decipher the output. What does this %*c mean? Any help would be appreciated.

int dd,mm,yy;
printf("\n\tEnter day,month and year");
scanf("%d %*c %d %*c %d",&dd,&mm,&yy);  // what does %*c mean ?
printf("\n\tThe date is : %d %d %d",dd,mm,yy);

输出

Enter day, month and year 23
2
1991
3
5
The date is: 23 1991 5

推荐答案

scanf() 格式中的 * 表示读取数据但不将其分配给参数列表中的变量'.在上下文中,这意味着您可以输入:

The * in a scanf() format means 'read the data but do not assign it to a variable in the argument list'. In context, it means you could type:

18/07/2012

并正确解释日 (18)、月 (7) 和年 (2012).格式字符串中的空格至关重要且使事情复杂化.通常,%c 读取下一个字符,甚至一个空格,但是 %*c 转换说明符周围的空格处理空格,因此代码需要一个非空格要消费的字符.

and get the day (18), month (7) and year (2012) interpreted correctly. The spaces in the format string are crucial and complicate things. Normally, %c reads the next character, even a space, but the spaces around the %*c conversion specifiers deal with white space, so the code needs a non-blank character to consume.

因此当您键入时观察到的行为:

Hence the observed behaviour that when you typed:

23 2 1991 3 5

第一个 %*c 消耗了 2(单独),第二个消耗了 3(单独).

the 2 (on its own) was consumed by the first %*c and the 3 (on its own) was consumed by the second.

这是标准 C,而不是 Turbo C 的特殊功能(问题的第一版指定了这一点,但自从我第一次写这个答案以来,该问题已被编辑以删除对 Turbo C 的引用.

This is Standard C and not a peculiar feature of Turbo C (which the first edition of the question specified, but the question has been edited to remove the reference to Turbo C since I first wrote this answer).

这篇关于scanf() 中的 %*c - 这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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