Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

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

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("
Enter day,month and year");
scanf("%d %*c %d %*c %d",&dd,&mm,&yy);  // what does %*c mean ?
printf("
The 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...