8

I am printing out (printf) the name of the function as I enter it by using the "__FUNCTION__" predefined macro (in gcc and clang). However, if I use -Wpedantic, I get this warning:

warning: ISO C does not support ‘__FUNCTION__’ predefined identifier [-Wpedantic]

How do I silence that warning?

Scooter
  • 6,802
  • 8
  • 41
  • 64

4 Answers4

11

There is no reason to use __FUNCTION__.

__func__ is the standard one (C99, C11, C17). C11 6.4.2.2p1:

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

    static const char __func__[] = "function-name";
    

From GCC documentation:

__FUNCTION__ is another name for __func__, provided for backward compatibility with old versions of GCC.

And if you want to know how old, __func__ appeared in GCC 2.95, released July 31, 1999. Mind you, you do not need __FUNCTION__ for anything else but to support GCC 2.94 or earlier. If you do, then that warning is probably least of your worries.


However, __func__ isn't available in C89/90 mode either, so you'd get a warning there. If you care about ISO diagnostics, then you need to use a more recent revision. Modern GCCs default to GNU C11 or C17 already.


See also: What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__

7

The standard compliant function identifier is __func__

From §6.4.2.2 of the C11 specification

The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration
static const char __func__[] = "function-name";
appeared, where function-name is the name of the lexically-enclosing function.

I believe that __func__ was added in C99.

user3386109
  • 34,287
  • 7
  • 49
  • 68
2

The -Wpedantic option is used to:

Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.

__FUNCTION__ is a GCC extension. However __func__ is a predefined identifier in C11. I understand this is part of C99 as well.

Committee draft for C11 (N1570) states that:

6.4.2.2 Predefined identifiers
Semantics
1. The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char _ _func_ _[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.

P.W
  • 26,289
  • 6
  • 39
  • 76
0

Do no use -Wpedantic unless you try to adhere to the ANSI C standard, which obviously does not support the __FUNCTION__ keyword.

Use -Wall -Wextra instead.

marcolz
  • 2,880
  • 2
  • 23
  • 28
  • Thanks, I mistakenly thought that pedantic had some function other than ANSI C compatibility. – Scooter Oct 24 '18 at 07:12
  • 2
    It does have functions other than ANSI C compatibility and you would be well served leaving it in your compile string and addressing the error with a preprocessor conditional that provides `_FUNCTION_` in the event it is not provided by your compiler (you may want to add `-Wshadow` as well) – David C. Rankin Oct 24 '18 at 07:18