Search code examples
clanguage-lawyerc-preprocessor

Defining macros with the same name as standard library functions


Does ISO C impose any restrictions on defining macros with the same name as standard library functions?

For example:

#define feof ...

Does the above have a problem?

If so, how about changing the case:

#define FEOF

If there is a problem, surely the rule (if any) would not be applicable to FEOF as feof and FEOF are two distinct identifiers, or is there something awry about my guesswork?


Solution

  • Does ISO C impose any restrictions on defining macros with the same name as standard library functions?

    Yes, from https://port70.net/~nsz/c/c11/n1570.html#7.1.3 :

    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.

    ....

    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 (see 7.1.4).

    ...

    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.

    2 No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

    3 If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.

    Identifier feof is listed as file scope identifier inside subclouse stdio.h.

    Defining feof is undefined behavior if stdio.h is included.

    FEOF is not listed, it is not a problem.