%*c in scanf() - what does it mean?

2020-02-10 09:35发布

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

OUTPUT

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

标签: c scanf
4条回答
forever°为你锁心
2楼-- · 2020-02-10 09:50

It means to ignore the next character such as a space, /, or a - that is common in written dates.

查看更多
在下西门庆
3楼-- · 2020-02-10 09:51

Use * with scanf suppresses assignment. The result of the conversion that follows is discarded.

查看更多
疯言疯语
4楼-- · 2020-02-10 09:54

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

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

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

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).

查看更多
地球回转人心会变
5楼-- · 2020-02-10 09:54

The * after % in a format string signify that the input matching the format will be ignored (thus no need to pass in a pointer to a variable to store the matched value that you are not going to use).

查看更多
登录 后发表回答