51

Why does the string returned by ctime() have a line feed (0x0A) as its final character? For example, this code:

#include <iostream>
#include <cstdlib>

int main(int argc, char* argv[])
{
    time_t now;
    time(&now);
    char* time_str = ctime(&now);
    std::cout << time_str << "why is this on a new line?" << std::endl;
    return 0;
}

...produces the following output:

$ ./time.exe
Wed Oct 23 14:52:29 2013
why is this on a new line?
$ 

It's not a big deal; I can strip the final byte from the string, but why does ctime() put it there in the first place?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
bythescruff
  • 1,869
  • 2
  • 17
  • 33
  • Just note: `0x0A` is a line-feed, not a carriage-return. – Angew is no longer proud of SO Oct 23 '13 at 14:16
  • I've seen this question debated before with no conclusive reasoning coming out of it. There's definitely no explicit standard for doing this. It seems to be just the way it was implemented. – dsell002 Oct 23 '13 at 14:20
  • 3
    @ShafikYaghmour yes, that's exactly what I said. It's there because it was there in the previous standard, which still doesn't give a good reason as to why it was there in the first place. – dsell002 Oct 23 '13 at 14:25
  • Thanks for the correction and edits; apparently I'm working too hard to remember my ASCII. :-) – bythescruff Oct 24 '13 at 09:51

6 Answers6

48

According to the C99 Rationale, the new line exists because of existing practice, which I think it's the same as saying for historical reasons.

Rationale for International Standard — Programming Languages — C §7.23.3.1 The asctime function

Although the name of this function suggests a conflict with the principle of removing ASCII dependencies from the Standard, the name was retained due to prior art. For the same reason of existing practice, a proposal to remove the newline character from the string format was not adopted.

This talks about asctime, but since ctime is equivalent to asctime(localtime(timer)), so the same rule applies.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
8

The POSIX Standard claims historical compatibility:

[asctime] is included for compatibility with older implementations ... Applications should use strftime() to achieve maximum portability.

Given that it was included for compatibility with older implementations, it's reasonable to assume that some older library implemented asctime with a newline at the end

SheetJS
  • 22,470
  • 12
  • 65
  • 75
4

This behaviour is required as defined in the ISO 9899:1990 specification.

7.12.3.1 The asctime function

The asctime function converts the broken-down time in the structure
pointed to by timeptr into a string in the form

         Sun Sep 16 01:03:52 1973\n\0

7.12.3.2 The ctime function

The ctime function converts the calendar time pointed to by timer to
local time in the form of a string.  It is equivalent to 

         asctime(localtime(timer))
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
4

It could be because originally it was required to implement the date program in Unix. (So maybe a newline for the shell)? So for historical reasons maybe.

Sadique
  • 22,572
  • 7
  • 65
  • 91
3

If you want your own format (one without the newline), use strftime() instead. The format string "%c" should give you roughly the same format but without the newline.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
0

The asctime() man page does mention (but does not emphasize) that the returned string has a terminating newline character before the terminating null character. Why this information is not also present in the ctime man page is a mystery.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
JamesU
  • 1