why does gcc fail to compile `printf("1 -- " __FUNCTION__ "\n")' ?

Dan Hipschman dsh@linux.ucla.edu
Tue Jul 1 03:17:00 GMT 2008


On Tue, Jul 01, 2008 at 10:55:18AM +0800, PRC wrote:
> Hi All,
> 
> I don't understand why gcc is unable to compile the following line:
> printf("1 -- " __FUNCTION__ "\n");
> 
> but gcc can compile this line:
> printf("1 -- %s\n", __FUNCTION__);
> 
> what's different between these two lines since __FUNCTION__ is a predefined macro which works as a const string?

It's not a string literal.  See:

http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html

Conceptually, the reason this need not work is that string concatenation
is done before syntatic analysis by the compiler (this is part of the
standard), so at the time adjacent string literals are concatenated, the
compiler may know nothing about what function, if any, the code is in.



More information about the Gcc-help mailing list