Skip to main content
GameDev.net gamedev.net
🔒 Locked

Simple C question

Started by quant Jun 28, 2004 at 2:03 PM 2 replies 450+ views
Original Post
quant
quant
fscanf(stdin, "%2d%f%*d %[0123456789]", &i, &x, name);
What is the function of these scansets (the []), i cant find a clear answer anywhere.
-keyboard
felisandria
felisandria
From MSDN:

To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

In other words, it should read everything so long as it's numeric, basically, in your example.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
quant
quant
So would it just discard these read values?
-keyboard
felisandria
felisandria
I imagine it would read them into your buffers (the variables after your argument, i, x, and name). Generally scanf just stops when it runs out of what's assigned by the argument.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.