Reputation: 1289
Is it correct to use scanf() as follows:
int n;
scanf("[%d]", &n);
If so, what does it do?
Upvotes: 0
Views: 348
Reputation: 3381
scanf("[%d]", &n);
Will extract the int a a given line if the string is something like:[1234]
It will extract the value 1234 and store it in the int.
Upvotes: 2
Reputation: 400059
It reads a square bracket (the [
), then a converts a decimal integer and stores the result in n
, then reads a closing square bracket. The brackets themselves are simply skipped over, they're not part of the conversion.
You might be thinking of the character group conversion specifier, which looks like %[...]
, where the ellipsis should be replaced by characters to accept. See the manual page for details.
Upvotes: 5