Re: __func__ is not a keyword

Liste des GroupesRevenir à cl c 
Sujet : Re: __func__ is not a keyword
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 15. Mar 2025, 22:20:41
Autres entêtes
Organisation : None to speak of
Message-ID : <87bju2htxy.fsf@nosuchdomain.example.com>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13)
Thiago Adams <thiago.adams@gmail.com> writes:
This program does not compile..in gcc and clang
>
int __func__  i =1;
>
int main () {}
>
error: expected identifier...

The "i" makes that a syntax error anyway, even if "__func__" were
accepted as an ordinary identifier.  Still, you have found something
odd.

Standard has..
>
"The identifier __func__ shall be implicitly declared by the
translator as if, immediately following
the opening brace of each function definition, the declaration" ...
>
>
My understand is that __func__   is not a keyword and that is
something defined inside the functions.. so I don’t know why gcc and
clang complains in the file scope.

If I change your program to:

    int __func__ = 1;
    int main(void) {}

both gcc and clang complain "error: expected identifier or ‘(’".
If I change it from __func__ to __foo__, neither gcc nor clang
complains.

    All identifiers that begin with a double underscore (__) or begin
    with an underscore (_) followed by an uppercase letter are reserved
    for any use, except those identifiers which are lexically identical
    to keywords.
    ...
    If the program declares or defines an identifier in a context in
    which it is reserved (other than as allowed by 7.1.4), the behavior
    is undefined.

So the program has undefined behavior, which means that terminating
the translation with the issuance of a diagnostic message is
permitted.

I'm mildly curious how gcc and clang treat "__func__" internally
that leads to this odd behavior.  The obvious way to implement it
would be to internally create a declaration of __func__ on entry
to each function definition, which shouldn't cause the symptom
you're seeing.  But it's not a conformance issue

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Date Sujet#  Auteur
15 Mar 25 * __func__ is not a keyword15Thiago Adams
15 Mar 25 `* Re: __func__ is not a keyword14Keith Thompson
15 Mar 25  +* Re: __func__ is not a keyword3Thiago Adams
15 Mar 25  i`* Re: __func__ is not a keyword2Keith Thompson
15 Mar 25  i `- Re: __func__ is not a keyword1Keith Thompson
16 Mar 25  +* Re: __func__ is not a keyword3Kaz Kylheku
16 Mar 25  i`* Re: __func__ is not a keyword2Keith Thompson
18 Mar 25  i `- Re: __func__ is not a keyword1Tim Rentsch
16 Mar 25  `* Re: __func__ is not a keyword7Keith Thompson
16 Mar 25   `* Re: __func__ is not a keyword6Kaz Kylheku
16 Mar 25    +* Re: __func__ is not a keyword4Keith Thompson
17 Mar 25    i`* Re: __func__ is not a keyword3Kaz Kylheku
17 Mar 25    i `* Re: __func__ is not a keyword2Keith Thompson
17 Mar 25    i  `- Re: __func__ is not a keyword1Kaz Kylheku
16 Mar 25    `- Re: __func__ is not a keyword1James Kuyper

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal