问题
The oldest publicly available C
standard seems to be the ISO/IEC 9899:1999, published in 1999.
There the strcpy
function already has its current syntax.
The reason I ask it is that I found a really, really old system where (to my greatest shock) strcpy
seems to have almost the same syntax as memcpy
.
In that implementiation strcpy behaves like this:
strcpy(char *destination, char *source, int length)
works just like the modernmemcpy
strcpy(char *destination, char *source)
does compile, but it doesn't have any noticeable side-effect, just as if it was called withlength = 0
.strncpy
works exactly just likestrcpy
, it even compiles with just 2 parameters.
So, for someone interested in C-archeology (and having access to very old standards), was there a C standard where strcpy
was not defined? Which was the first standard where it was defined? Am I dealing with a non-standard conforming compiler (a compiler that doesn't even conform to the oldest C standard)?
I couldn't find any indication which version of C is really supposed to be implemented, the only hint is that it was written before 1997 (as I found the text "Copyright 1997") for a Fujitsu processor.
Edit: If this source is to be trusted, strcpy
was already defined in its current form already in 1989. So I have to deal with something even older than I first suspected!
回答1:
The first published standard for C was C89 (ANSI X3.159-1989). It defined strcpy
as follows:
char *strcpy(char *s1, const char *s2);
Before, that, try looking for the first version of "The C Programming Language" by Kernighan and Ritchie, which was the de facto standard from its publication in 1978 to 1989. I would not be surprised if strcpy
predates this.
According to http://cm.bell-labs.com/7thEdMan/, Version 7 UNIX, released in 1979, defined strcpy as
char *strcpy(s1, s2)
char *s1, *s2;
I wasn't able to find strcpy
in the documentation for Version 6 UNIX, which was released in 1975, but its "C Manual", written by Dennis Ritchie, contained an example function
copy(s1, s2)
char *s1, *s2
{
while(*s2++ = *s1++);
}
Its "C Tutorial", written by Brian Kernighan, instead contained this version:
strcopy(s1, s2)
char s1[ ], s2[ ]; {
int i;
for( i = 0; (s2[i] = s1[i]) != '\0'; i++ );
}
This suggests that the modern version of strcpy
originated somewhere between the release of V6 UNIX in 1975 and the publication of K&R C in 1978.
Notice that even at this early time, we already see stylistic differences between the two principle authors of C: Kernighan put the opening bracket of functions on the same line as the parameter declaration, while Ritchie preferred to put it on a new line by itself.
回答2:
strcpy
is defined in K&R C dating to 1978.
Here it is, p88:
/* strcpy: copy t to s; pointer version */
void strcpy(char *s, char *t)
{
int i;
i = 0;
while ((*s = *t) != '\0') {
s++;
t++;
}
}
Or, as reduced in the text of the greatest programming book ever written:
/* strcpy: copy t to s; pointer version 3 */
void strcpy(char *s, char *t)
{
while (*s++ = *t++)
;
}
来源:https://stackoverflow.com/questions/25300040/is-it-possible-to-find-out-when-the-current-syntax-of-strcpy-was-added-to-the-c