Re: calling my custom function same as library function

From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/28/05


Date: Sun, 27 Feb 2005 21:51:15 -0600

On 28 Feb 2005 02:48:17 GMT, roberson@ibd.nrc-cnrc.gc.ca (Walter
Roberson) wrote in comp.lang.c:

> In article <1109555739.136783.312850@l41g2000cwc.googlegroups.com>,
> Deniz Bahar <deniz.bahar@gmail.com> wrote:
> :I would like to call one of my functions the exact name as an existing
> :C library function (for example K&R2 exercises asks me to make an atof
> :function). If I don't include the header with the declaration for the
> :C library function (stdlib.h in this case) then define/declare my own
> :function with the same name, am I safe?
>
> Library function names are not 'reserved'.

The C standard disagrees:

"7.1.3 Reserved identifiers
1 Each header declares or defines all identifiers listed in its
associated subclause, and optionally declares or defines identifiers
listed in its associated future library directions subclause and
identifiers which are always reserved either for any use or for use as
file scope identifiers."

...and then, in the itemized list that is also part of paragraph 1:

"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."

...therefore, it is undefined to define your own function with the
same name as any standard library function, unless you use the static
keyword to limit it to internal linkage.

Whether or not this particular instance of undefined behavior "works"
is in the eye of the beholder, since the C standard requireth not.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: Disable printf
    ... of C99 also says "All identifiers with external linkage in any ... of the following subclauses (including the future library directions) ... It's not fine in C99, even if you don't use anything from. ...
    (comp.lang.c)
  • Re: Disable printf
    ... of C99 also says "All identifiers with external linkage in any ... of the following subclauses (including the future library directions) ... It's not fine in C99, even if you don't use anything from. ...
    (comp.lang.c)
  • Re: Warnings with gcc
    ... programs are not allowed to declare such identifiers with external linkage even if they *don't* include string.h ... | use as a macro name and as an identifier with file scope in the same ...
    (comp.lang.c)
  • Re: calling my custom function same as library function
    ... >1 Each header declares or defines all identifiers listed in its ... >associated subclause, and optionally declares or defines identifiers ... >subclauses (including the future library directions) are always ... blah, blah, blah ...
    (comp.lang.c)
  • Re: [C++] ceil() outside <cmath>
    ... >specified in the standard are reserved under all conditions? ... that all identifiers, listed in a library subclause, must be declared ... Macro names cannot be used if their associated header is included. ... All identifiers with external linkage, such as strlen, cannot be used ...
    (alt.comp.lang.learn.c-cpp)