(Some of this answer is opinionated. Sorry. I have opinions.)
scanf
is really not the ideal tool for precise verification of input. In particular, the fact that it does not normally distinguish between newlines and other whitespace makes it really very difficult to validate strictly line-oriented input.
On the other hand, if you've got all that punctuation in there, maybe you should be prepared to accept more free-form input. Why shouldn't the user enter the data on two lines, or even five lines:
{
1,
2,
3
}
(Or pythonesquely:
{ 1
, 2
, 3
}
:-) )
Unless you go to heroic lengths to forbid them, or use the fgets/sscanf
workaround usually suggested here, all of the above will be accepted. Which might make some user happy, you never know.
However, there is another issue which is possibly worth trying to solve. Here, you want to ensure that the triple is correctly terminated with a close brace }
, and you can't do that by just putting }
into the pattern. If you need to validate a character which comes after the the last conversion, you need to make that character a conversion (so that it becomes the last conversion). Otherwise, scanf
will simply leave the unmatched input character in the input stream and report that all data conversions were successful.
So you could use something like this:
int a,b,c;
char brace;
if (scanf("{ %d , %d , %d %c", &a, &b, &c, &brace) != 4
|| brace != '}') {
printf("Bad format");
}
That guarantees that there is a }
, possibly preceded by whitespace. However, it
doesn't guarantee that the }
is the last thing on the line. Since it carefully does not skip whitespace after the pattern, you could check that the rest of the line is empty by reading it with fgets
(which will read up to and including the trailing newline character), and then checking the characters read to make sure they all satisfy isspace()
.
Or you could just let the user put newlines wherever they want to.