Implementing a new strcpy function redefines the library function strcpy?
c
Solution
C11(ISO/IEC 9899:201x) §7.1.3 Reserved Identifiers
— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise.
— All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.
— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.
If the program declares or defines an identifier in a context in which it is reserved, or defines a reserved identifier as a macro name, the behavior is undefined. Note that this doesn't mean you can't do that, as this post shows, it can be done within gcc and glibc.
glibc §1.3.3 Reserved Names proveds a clearer reason:
The names of all library types, macros, variables and functions that come from the ISO C standard are reserved unconditionally; your program may not redefine these names. All other library names are reserved if your program explicitly includes the header file that defines or declares them. There are several reasons for these restrictions:
Other people reading your code could get very confused if you were using a function named exit to do something completely different from what the standard exit function does, for example. Preventing this situation helps to make your programs easier to understand and contributes to modularity and maintainability.
It avoids the possibility of a user accidentally redefining a library function that is called by other library functions. If redefinition were allowed, those other functions would not work properly.
It allows the compiler to do whatever special optimizations it pleases on calls to these functions, without the possibility that they may have been redefined by the user. Some library facilities, such as those for dealing with variadic arguments (see Variadic Functions) and non-local exits (see Non-Local Exits), actually require a considerable amount of cooperation on the part of the C compiler, and with respect to the implementation, it might be easier for the compiler to treat these as built-in parts of the language.
Problem
It is said that we can write multiple declarations but only one definition. Now if I implement my own strcpy function with the same prototype : ``` char * strcpy ( char * destination, const char * source ); ``` Then am I not redefining the existing library function? Shouldn't this display an error? Or is it somehow related to the fact that the library functions are provided in object code form? EDIT: Running the following code on my machine says "Segmentation fault (core dumped)". I am working on linux and have compiled without using any flags. ``` #include <stdio.h> #include <stdlib.h> #include <string.h> char *strcpy(char *destination, const char *source); int main(){ char *s = strcpy("a", "b"); printf("\nThe function ran successfully\n"); return 0; } char *strcpy(char *destination, const char *source){ printf("in duplicate function strcpy"); return "a"; } ``` Please note that I am not trying to implement the function. I am just trying to redefine a function and asking for the consequences. EDIT 2: After applying the suggested changes by Mats, the program no longer gives a segmentation fault although I am still redefining the function. ``` #include <stdio.h> #include <stdlib.h> #include <string.h> char *strcpy(char *destination, const char *source); int main(){ char *s = strcpy("a", "b"); printf("\nThe function ran successfully\n"); return 0; } char *strcpy(char *destination, const char *source){ printf("in duplicate function strcpy"); return "a"; } ```