What should happened if we use predefined variable __func__
outside a function in C (C99 / C11) and C++?
#include <stdio.h>
const char* str = __func__;
int main(void)
{
printf("%s", str);
return 0;
}
gcc 4.7.2 only give a warning (with -Wall -W -pedantic
enabled) and prints nothing.
Standard doesn't say anything about it explicitly:
ISO/IEC 14882:2011
8.4.1 In general [dcl.fct.def.general]
8 The function-local predefined variable
__func__
is defined as if a definition of the formstatic const char __func__[] = "function-name";
had been provided, where function-name is an implementation-defined string. It is unspecified whether such a variable has an address distinct from that of any other object in the program.
ISO/IEC 9899:2011
6.4.2.2 Predefined identifiers
1 The identifier
__func__
shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declarationstatic const char __func__[] = "function-name";
appeared, where function-name is the name of the lexically-enclosing function.
UB? Error? Or something else?