Function Signatures In time.h

A

August Karlstrom

Hi,

Does anyone know why some of the functions in time.h use pointers to
constant objects of type time_t when time_t is an aritmetic type. Why is
e.g. ctime declared as

char* ctime(const time_t* tp);

and not as

char* ctime(time_t t);

?


Regards,

August
 
D

Diomidis Spinellis

August said:
Does anyone know why some of the functions in time.h use pointers to
constant objects of type time_t when time_t is an aritmetic type. Why is
e.g. ctime declared as

char* ctime(const time_t* tp);

and not as

char* ctime(time_t t);

My guess is that this is an implementation decision related to the
environment where C has its roots. In the Seventh Edition Unix (and
probably also in earlier versions), time_t is implemented as a long
<http://minnie.tuhs.org/UnixTree/V7/usr/include/sys/types.h.html>. On a
PDP-11 where that 1979 version of Unix run, passing as an argument a 16
bit pointer to a 32 bit long was probably more efficient than passing
the actual 32 bit value.
 
A

August Karlstrom

Diomidis said:
My guess is that this is an implementation decision related to the
environment where C has its roots. In the Seventh Edition Unix (and
probably also in earlier versions), time_t is implemented as a long
<http://minnie.tuhs.org/UnixTree/V7/usr/include/sys/types.h.html>. On a
PDP-11 where that 1979 version of Unix run, passing as an argument a 16
bit pointer to a 32 bit long was probably more efficient than passing
the actual 32 bit value.
OK, thanks for the input. On the other hand, the function difftime

double difftime(time_t t2, time_t t1);

does not use pointer arguments.


August
 
K

Keith Thompson

August Karlstrom said:
OK, thanks for the input. On the other hand, the function difftime

double difftime(time_t t2, time_t t1);

does not use pointer arguments.

Probably difftime() wasn't added until later.

I *think* that on some early implementations, 32-bit ints weren't
directly supported, and what's now a ftime_t was probably defined as
an array of two ints. The declaration of time() may have been
something like:

time(int t[2]);

Certainly much of the C library would be different if it were being
defined from scratch today, without concern for backward
compatibility.
 
D

Diomidis Spinellis

Keith said:
[...]
I *think* that on some early implementations, 32-bit ints weren't
directly supported, and what's now a ftime_t was probably defined as
an array of two ints. The declaration of time() may have been
something like:

time(int t[2]);

You are absolutely right. In the Third Edition Unix (February 1973) the
time(2) interface is specified in assembly language: the 32-bit result
is returned in the register pair r0/r1
<http://minnie.tuhs.org/UnixTree/V3/usr/man/man2/time.2.html>. In the
Fourth Edition Unix (November 1973), which was (re)written in C, time
takes as an argument an int tvec[2]
<http://minnie.tuhs.org/UnixTree/V4/usr/man/man2/time.2.html>. Ritchie
mentions that the long type was added to C during the period 1973-1980
<http://cm.bell-labs.com/cm/cs/who/dmr/chist.html>. The long type
certainly wasn't supported by the C compiler that came with the Fifth
Edition Unix that was released on June 1974
<http://minnie.tuhs.org/UnixTree/V5/usr/c/c00.c.html>. Therefore, when
time(2) was first specified in C, an int[2] argument was a reasonable
interface specification.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top