Reputation: 793
I am going through some examples in a book I have and I have come to something I've never seen before nor understand:
scanf("%d-%d-%d-%d-%d", &prefix, &group, &publisher, &item, &check_digit);
This code is part of a program that asks the user to enter their ISBN number of a book and later on breaks down the ISBN into Prefix = x
, Group = y
, etc..
I have NEVER seen the hypens between the %d
's. Does anyone see any point in this??
Thanks
Upvotes: 3
Views: 351
Reputation: 80031
scanf
takes a format string, so it's about what input it expects to see for input. The hyphens are for actual hyphens in the input. 1-2-3-4-5-6
would be valid input for this call, and give you prefix=1
, group=2
, etc.
Upvotes: 1
Reputation: 12960
It parses 5 numbers (matched by %d
) with a dash between each two (the -
). Each number is saved to a variable passed in as argument. See the manual for scanf
and printf
under Conversions
.
Upvotes: 1
Reputation: 79910
The represent the actual parts of the string to be scanned.
For example your "%d-%d-%d-%d-%d"
would work with something like "10-56-666-1-90"
.
Upvotes: 3
Reputation: 131867
"Pattern matching". If the input doesn't fit the specified pattern (also called format), it fails. So if you input anything else than INT-INT-INT-INT-INT
(where INT
is a placeholder for an integer you input), the input would be considered invalid.
Upvotes: 4