• Re: __func__ is not a keyword

    From Mario Rosell@3:633/10 to All on Fri Apr 17 20:19:39 2026
    __func__ is just an string that has name of the function it is in,
    so for example:

    #include <stdio.h>
    int
    main(int argc, char** argv) {
    printf("we are on: %s\n", __func__);
    return 0;
    }

    Will print:

    we are on: main

    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.

    Yes, it is a sort of atomic identified reserved by the compiler.
    You can't redefine it, just like any normal identifier, if that
    makes sense. The compiler errors are from C not understanding
    the code.

    "The identifier __func__ shall be implicitly declared by the translator
    as if, immediately following
    the opening brace of each function definition, the declaration" ...

    What it refers to is:

    type
    myfunction(...) { // <--- from here on, __func__ is available.
    ...
    }

    --
    "I know thy works, that thou art neither cold nor hot: I would thou wert
    cold or hot. So then because thou art lukewarm, and neither cold nor
    hot, I will spue thee out of my mouth." (Rev. 3:15-16)

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Keith Thompson@3:633/10 to All on Fri Apr 17 14:15:48 2026
    You're replying to an article posted by Thiago Adams more than a
    year ago.

    You deleted the attribution line. Please don't do that.

    Mario Rosell <usenet@mariorosell.es> writes:
    __func__ is just an string that has name of the function it is in,
    so for example:

    #include <stdio.h>
    int
    main(int argc, char** argv) {
    printf("we are on: %s\n", __func__);
    return 0;
    }

    Will print:

    we are on: main

    It's not *just* a string. It's an implicitly defined static array of
    char.

    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.

    Yes, it is a sort of atomic identified reserved by the compiler.
    You can't redefine it, just like any normal identifier, if that
    makes sense. The compiler errors are from C not understanding
    the code.

    "The identifier __func__ shall be implicitly declared by the translator
    as if, immediately following
    the opening brace of each function definition, the declaration" ...

    What it refers to is:

    type
    myfunction(...) { // <--- from here on, __func__ is available.
    ...
    }

    This was all discussed at the time, and more accurately than in your
    response.

    The code in the original message was:

    int __func__ i =1;

    which is a syntax error even if a non-reserved identifier is used.
    It's likely that Thiago meant to write:

    int __func__ = 1;

    But that has undefined behavior, because __func__ is a reserved
    identifier due to the double underscore.

    There are some oddities in the way gcc and clang treat __func__.
    If you're curious, you can look up the thread from last year.
    (gcc seems to treat __func__ as a keyword, which it isn't, but
    that doesn't seem to cause any conformance issues, just some odd
    wording in some diagnostics for bad code.)

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

    --- PyGate Linux v1.5.14
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)