Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #391252

Re: __func__ is not a keyword

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: __func__ is not a keyword
Date 2025-03-15 14:20 -0700
Organization None to speak of
Message-ID <87bju2htxy.fsf@nosuchdomain.example.com> (permalink)
References <vr4lgu$63fu$1@dont-email.me>

Show all headers | View raw


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 */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

__func__ is not a keyword Thiago Adams <thiago.adams@gmail.com> - 2025-03-15 16:47 -0300
  Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-15 14:20 -0700
    Re: __func__ is not a keyword Thiago Adams <thiago.adams@gmail.com> - 2025-03-15 18:30 -0300
      Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-15 14:53 -0700
        Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-15 15:04 -0700
    Re: __func__ is not a keyword Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-16 07:18 +0000
      Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-16 02:06 -0700
        Re: __func__ is not a keyword Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-03-18 10:00 -0700
    Re: __func__ is not a keyword scott@slp53.sl.home (Scott Lurndal) - 2025-03-16 15:51 +0000
      Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-16 11:51 -0700
        Re: __func__ is not a keyword Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-16 19:05 +0000
          Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-16 13:29 -0700
            Re: __func__ is not a keyword Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-17 17:03 +0000
              Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-17 13:09 -0700
                Re: __func__ is not a keyword Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-17 20:31 +0000
          Re: __func__ is not a keyword James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-03-16 16:35 -0400
  Re: __func__ is not a keyword Mario Rosell <usenet@mariorosell.es> - 2026-04-17 20:19 +0000
    Re: __func__ is not a keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-04-17 14:15 -0700

csiph-web