1

I'm trying to handle user input for 2 following commands:

quit
open <n>

where is an integer.

Right now, my solution is the following:

char input_string[10];
int n;
int trail_index;
//<user input here>
sscanf(input_string, "%s%n %d%n", command, &trail_index, &n, &trail_index);

The trail_index is assigned correctly for me (4 in case of quit command, 6 in case of "open 1"), but since the program may be used with different compilers and platforms, the question is: is the behavior of sscanf guaranteed to work this way when you use the same variable multiple times, or is this undefined behavior that just happens to work for Visual C?

fwiffo
  • 45
  • 6
  • 2
    See [Is scanf(“%d%d”, &x, &x) well defined?](https://stackoverflow.com/q/35712349/2410359) – chux - Reinstate Monica Aug 06 '18 at 15:55
  • The space in the format string is redundant since `%d` skips spaces and whitespace in a format string is optional (as opposed to `%*[ ]`). But note that the format string could ingest something that looks like a number on the line following the `quit` command. Consequently, this will not play well with interactive uses, because it forces `scanf` to read the next line after the quit command to see if a number might match, which means that typing `quit` will not actually quit. (`quit` will work, but users will be puzzled.) – rici Aug 06 '18 at 21:35
  • @rici, I'm handling command format checks separately. They were just removed from the question for the sake of readability since they are not directly related to the main question. Thanks for this information, though. – fwiffo Aug 20 '18 at 10:04
  • Also note that I'm not actually using `scanf`, but rather `sscanf`. – fwiffo Aug 27 '18 at 10:12

1 Answers1

2

From this scanf (and family) reference

There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.

So this is indeed well-defined behavior and allowed.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    Is there a true, full sequence point? The question and answer that @chux linked in the comments above doesn't seem go quite that far, and my reading of [7.21.6.2 The fscanf function](https://port70.net/~nsz/c/c11/n1570.html#7.21.6.2), doesn't seem to require an actual sequence point. A full sequence point seems to me to have implications beyond the order format specifiers are evaluated. (And yes, this is a language-lawyer rabbit hole...) – Andrew Henle Aug 06 '18 at 16:04
  • @AndrewHenle While informative only, [Annex C](https://port70.net/~nsz/c/c11/n1570.html#C) says "After the actions associated with each formatted input/output function conversion specifier". So it should be possible to puzzle it together from the standard. – Some programmer dude Aug 06 '18 at 16:42
  • 1
    @AndrewHenle: You need to read the introductory [7.21.6](https://port70.net/~nsz/c/c11/n1570.html#7.21.6p1) as well. It consists of a single sentence "The formatted input/output functions shall behave as if there is a sequence point after the actions associated with each specifier" with a footnote pointing out that `%n` is also a specifier (relevant to this question). – rici Aug 06 '18 at 21:29