1

The documentation for gets says:

Reads characters from the standard input (stdin) and stores them as a C string into str until a newline character or the end-of-file is reached.

But it doesn't say what the newline character is. Are all of the following considered a newline character: '\r' or '\n' or '\r\n'?

user247763
  • 191
  • 5
  • 15
  • 1
    `gets` is obsolete, please don't use it. With `fgets` only one of a pair of EOL markers becomes part of the input string (Windows). – Weather Vane May 24 '17 at 18:14
  • It stops when it hits a newline or overwrites something important and segfaults, which is why every C reference says don't use gets. – stark May 24 '17 at 18:14
  • 1
    the question would be equally valid for `fgets()`, and yes, do yourself a favor and only use `fgets()`. –  May 24 '17 at 18:16
  • 3
    The answer [depends on the environment](https://stackoverflow.com/a/18383562/335858). – Sergey Kalinichenko May 24 '17 at 18:23
  • 2
    `gets()` reads from `stdin`, which by default is open as a text stream. One of the characteristics of a text stream is that some implementation-defined sense of line termination will be converted to newline characters on the fly. – John Bollinger May 24 '17 at 18:33
  • 5.2.2 Character display semantics p2 \n (new line) Moves the active position to the initial position of the next line. \r (carriage return) Moves the active position to the initial position of the current line – BLUEPIXY May 24 '17 at 18:43
  • Also 7.4.1.10 The isspace function ...new-line ('\n'),... – BLUEPIXY May 24 '17 at 19:12
  • Possible duplicate of [Difference between \n and \r?](https://stackoverflow.com/questions/1761051/difference-between-n-and-r) – old_timer Jun 15 '17 at 20:41

1 Answers1

1

From the C Standard (5.2.2 Character display semantics)

\n (new line) Moves the active position to the initial position of the next line.

And (7.21.2 Streams)

2 A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined. Characters may have to be added, altered, or deleted on input and output to conform to differing conventions for representing text in the host environment. Thus, there need not be a one-to-one correspondence between the characters in a stream and those in the external representation. Data read in from a text stream will necessarily compare equal to the data that were earlier written out to that stream only if: the data consist only of printing characters and the control characters horizontal tab and new-line; no new-line character is immediately preceded by space characters; and the last character is a new-line character. Whether space characters that are written out immediately before a new-line character appear when read in is implementation-defined.

Thus the new line character is the character '\n'.

Take into account that the function gets is unsafe and is not supported any more by the C Standard.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335