__func__出错

时间:2011-05-22 19:06:06

标签: c string

#include <stdio.h>    
char* __func__ = "hello";
void myfunc(void)
{
      printf("%s\n", __func__);
}
int main()
{
  myfunc();
}

上面的代码段显示错误:expected identifier or ‘(’ before ‘__func__’。为什么呢?

2 个答案:

答案 0 :(得分:9)

您知道__func__是预定义的标识符(至少从C99开始,有关详细信息,请参阅here)?你为什么用它?我猜你在__func__定义行得到了错误,因为那样。

答案 1 :(得分:0)

从技术上讲,根据C99,行为是未定义的,因为您明确声明了__func__

  

J.2未定义的行为
  在以下情况下,行为未定义:

     

...

     

标识符__func__已明确声明(6.4.2.2)。

众所周知,未定义的行为意味着任何事情都可能发生[并且还包括编译错误]