Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scanf() asterisk.

Status
Not open for further replies.

denc4

Programmer
Feb 17, 2005
107
NL
Hi.

I'm learning ANSI C for a few days now. I use Microsoft C 7.0 as a compiler, my target platform is DOS.
I'm having trouble grasping the idea of the asterisk in the scanf() function..

Why can't:

scanf("%i %*i %i",&value1,&value2,&value3);

be written as:

scanf("%i %i",&value1,&value2);

these two commands basically yield the same result, right?
then why bother with the asterisk to begin with?

thanks.
 
> scanf("%i %*i %i",&value1,&value2,&value3);
Which is wrong anyway, it should be
[tt]scanf("%i %*i %i",&value1,&value2);[/tt]

> scanf("%i %i",&value1,&value2);
Because this expects you to type in
[tt]12 34[/tt]

Whereas the assignment suppressed version expects
[tt]12 9999 34[/tt]

Both result in 12 and 34 being stored in the conversion results.

--
 
Consider that, instead of reading input from a user sitting at the keyboard, it might be reading the input from a file with predefined fields. You might only want the first and third fields, and not care about the second.
 
ok, but why ask the user to input 3 strings, if only 2 are stored?
in other words: why would you let the user input a string when it won't be assigned to a variable anyway?
 
that makes sense chipperMDW, i haven't gotten to the file I/O operations in my book yet.

so, when getting input from the keyboard the asterisk is actually useless.

thanx.
 
Hmm... I've never heard about that asterisk thing before. I would have just used a 3rd int and just ignored what was stored in it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top