3

Reading the famous book The C programming language ANSI C second edition by Brian Kernighan and Dennis Ritchie, I found in chapter 7 (section 7.4. page 157) this paragraph below which describe the format string for scanf:

[...]

The format string usually contains conversion specifications, which are used to control conversion of input. The format string may contain:

  • Blanks or tabs, which are ignored.

[...]

And I remembered that nowadays we use space in format string to tell the compiler to skip white space until it finds a non-white space character. So I assumed that this paragraph is no longer valid due to the updating of the C the language through the years. Is what I am saying is correct or not?

Community
  • 1
  • 1
  • 2
    You're definitely right that whitespace in a `scanf` format string has meaning. Without more context around that quote, I'm not sure whether it's wrong or you're just taking it out of context though. – Joseph Sible-Reinstate Monica May 09 '20 at 21:50
  • 1
    I'm not 100% certain I understand the question, but the easiest way to know is to check the standards for the libc functions in question online. You can find some help on sites such as this one: https://en.cppreference.com/w/c/io/fscanf – PhotonFalcon May 09 '20 at 21:53
  • @JosephSible-ReinstateMonica the paragraphe is very explicit about blanks and tabs it says they are ignored if they exists in the format string and there is more details – Ramzi Bouali May 09 '20 at 22:55
  • @PhotonFalcon it's very clear to me that white space are ignored by the compiler if he find it in the format string of scanf. this was the issue in the late 80's because the book which i quoted from it was written at that time. but nowdays a white space in a format string mean skip the white spaces in the input stream until reaching a non- white space. right ? so my question is : how this behavior of scanf was changed and when ? – Ramzi Bouali May 09 '20 at 23:03
  • @JosephSible-ReinstateMonica sorry i meant " there is no more details" instead of " there is more details" i forgot the "no" – Ramzi Bouali May 09 '20 at 23:05

1 Answers1

4

The C bible documents an obsolete version of scanf(). Early versions of scanf() used to ignore all white space in the input string, so white space in the format string were ignored too. This behavior was changed well before C was normalized by ANSI and later by ISO.

The book cover of the second edition does mention ANSI-C, but regarding scanf(), its description is incorrect for the ANSI and later versions.

As a matter of fact the man page from Version 7, the original Unix from Bell Labs in 1979 already documents this:

The control string usually contains conversion specifications, which are used to direct interpretation of input sequences. The control string may contain:

  1. Blanks, tabs or newlines, which match optional white space in the input.
  2. An ordinary character (not %) which must match the next character of the input stream.
  3. Conversion specifications, consisting of the character %, an optional assignment suppressing character *, an optional numerical maximum field width, and a conversion character.

No actual compilers support the ancient behavior documented in the book. After researching this surprising mistake in K&R, it seems scanf() has had the current behavior almost from day one of the Unix system. scanf() has always been quirky and error prone, this great finding adds to a long series of traps and pitfalls.

You can find a list of errata correcting some errors in the second edition of the book, but this particular one is not listed.

For further investigations, a lot of historic information can be found on Dennis Ritchie's home page, Brian Kernighan's page on the book, and here, and on bitsavers.org archives.

Community
  • 1
  • 1
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • So the actual compilers don't support the old behavior of scanf ? From which standard the behavior is changed ? – Ramzi Bouali May 09 '20 at 22:46
  • 1
    @RamziBouali: no actual compilers support this ancient behavior. Researching this surprising mistake in K&R, it seems `scanf()` has had the current behavior almost from day one of the Unix system. `scanf()` has always been quirky and error prone, this great finding adds to a long series of traps and pitfalls. – chqrlie May 09 '20 at 23:21
  • oh ! that's very surprising ! i thought K&R like a holy book . Now i think it may contain many others mistakes so it is better not count on it anymore . Thank you anway – Ramzi Bouali May 09 '20 at 23:25
  • 1
    @RamziBouali: K&R is still a very good book, but it is dated and there are more modern texts on the C language. Look at this question for a list: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – chqrlie May 09 '20 at 23:41