How does C99 (or C11) standard apply to explain this case of macro expansion? [closed]

StackOverflow https://stackoverflow.com/questions/18833179

  •  28-06-2022
  •  | 
  •  

Question

In particular, I'm trying to find a reference document for the accepted answer to this question: Macro and function with same name

Can anyone point to any good references for parenthesis use in functions declarations as shown in the aforementioned example?

Any references on syntax tips/tricks in C would be appreciated too!

Était-ce utile?

La solution

You are referencing a correct document in your comment (the C99 standard, http://open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, the newer C11 standard does not introduce any significant changes to these sections). The sections that deal with macro replacements are 6.10.3 and 6.10.3.4. In particular, the mechanism that makes infinite replacement loops impossible (which is the core of the mechanism described in the link you mentioned) is found in 6.10.3.4(2):

If the name of the macro being replaced is found during this scan of the replacement list (not including the rest of the source file’s preprocessing tokens), it is not replaced. Furthermore, if any nested replacements encounter the name of the macro being replaced, it is not replaced. These nonreplaced macro name preprocessing tokens are no longer available for further replacement even if they are later (re)examined in contexts in which that macro name preprocessing token would otherwise have been replaced.`

This part of 6.10.3(10) deals with the instances when the macro name is not expanded:

Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro).`

Note the 'followed by a (' bit. This implies (since the standard does not specify otherwise) that in the absence of a '(' the macro is not expanded and is interpreted as a C token.

A great resource for 'deciphering' some arcane aspects of the C standard is The New C Standard: An Economic and Cultural Commentary available at http://www.knosof.co.uk/cbook/cbook.html. A fair warning: it is a long book but well worth reading if one is into the arcana of C. It does not deal with macros though. A lighter reading is C: A Reference Manual (check out http://careferencemanual.com/) which does have a chapter on the subtleties of the C preprocessor.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top