5

I'm currently prepping myself for programming school by going through the textbook. There's this particular question which I don't understand and the textbook doesn't give the answer.

PS: I've learned some C++/C# online, but never go through proper-taught programming classes, so I'm struggling with some of the concepts.

Q: For each of the following pairs of scanf format strings, indicate whether or not the two strings are equivalent. If they are not, show how they can be distinguished.

A) "%d" versus " %d"
B) "%d-%d-%d" versus "%d -%d -%d"
C) "%f" versus "%f "
D) "%f,%f" versus "%f, %f"

First off, I don't even understand what the question is asking. What does the textbook mean by whether or not the 2 strings are 'equivalent'?

If they are, could someone explain the differences and possibly show me how they can be distinguished?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • you should be talking to your prof for math homework... but "equivalent" could considered a synonym for "equal". "would the output of these format strings be the same". yes/no, then explain HOW they're the same/different. – Marc B Jun 30 '16 at 14:31
  • Sorry to say, this question will be too broad to answer here.... you need a tutorial. – Sourav Ghosh Jun 30 '16 at 14:32
  • This question actually is very broad, but it's highest possible quality – xenteros Jun 30 '16 at 14:36
  • Hi, Marc B, thanks for the edit! But since I'm prepping myself, I've no lecturers to ask therefore I have to come to programming sites like this. And thanks to buld0zzr, I've used the question you mentioned to give myself some understanding before looking through some of the answers below! :) – Jason_Kalmatos Jul 01 '16 at 15:47

2 Answers2

3

Let us try A first: "%d" versus " %d", they are equivalent format strings for scanf().

" " will do the following. It never fails.
1) Scan and discard (skip) optional white-space.
2) After reading a non-white-space or end-of-file, if not (EOF), the last character read is put back into stdin.

"%d" itself will attempt 3 things (It can fail)
1) Scan and discard (skip) optional white-space.
2) Scan and convert numeric text representing a decimal integer.
3) After reading a non-numeric text or end-of-file, if not (EOF), the last character read is put back into stdin.

" %d" does both the above. It is the same result of just doing the 2nd with "%d".

With *scanf() specifiers note:

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier. C11 §7.21.6.2 8


B, C, D differences?

Mouse over for hint 1:

A " " before a scanf() specifier, except the 3 noted above, is an equivalent scanf() format as without it.

Mouse over for hint 2:

Only 1 of 3 equivalent.

Mouse over for hint 3:

Consider inputs:
"123 -456-789"
"123.456 x" What is the next character to be read?

B) "%d-%d-%d" versus "%d -%d -%d"
C) "%f" versus "%f "
D) "%f,%f" versus "%f, %f"

Answer:

Awww, Do you really want to be given the answer?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

From Wikipedia

whitespace: Any whitespace characters trigger a scan for zero or more whitespace characters. The number and type of whitespace characters do not need to match in either direction.

scanf is about keep consuming the input and get the thing you care about. The normal char in format string means it consumes the exactly same char, and do nothing else. %d, %f could skip the leading whitespace. So, sum it up, we got:

  • A, it is the same, because %d skip the leading space
  • B, %d-%d-%d is pretty strict, it reads an integer after exactly - and then another integer and so on, so it reads 1-2-3 well, 1- 2- 3 well, too, but it fails on 1 - 2 - 3. While on the other hand, %d -%d -%d first skip spaces, read an integer, skip spaces, expect char-, then skip spaces again, and so on...
  • C, trailing spaces does not make a difference
  • D, it is the same, because %f skip leading spaces, too

So the answer would be B.

delta
  • 3,778
  • 15
  • 22