__FUNCTION__ in another macro

M

Mockey Chen

I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "

int main()
{
printf(LOG_PREFIX "some thing.\n");

return 0;
}

I complie and got error:
% cc func.c
func_test.c: In function `main':
func_test.c:6: error: syntax error before "__FUNCTION__"

I want to use LOG_PREFIX macro automatice expand.
Is there any way to do it?

Thanks in advance.

--
Regards.
Mockey Chen
 
M

mdler

Hello

why so complicated



just do it like this

#include <stdio.h>

int main ()
{
printf("Current function <%s>\n",__FUNCTION__);
return 0;
}

doing things your way it won't work, strings can not be added to
eachother in C
There has to be always memory to hold it!

better is it to make a error function like this

void error_logger(char *functionName,char *mess)
{
printf("Current function <%s> %s\n",functionName,mess);
}


and call it like this

error_logger(__FUNCTION__,"something");


this You can put in a macro like this

#define ERROR_LOG(mess) error_logger(__FUNCTION__,mess)

Now you can call it like

ERROR_LOG("someThing");


I hope that now your quetion is anserd

Greetings Olaf


Mockey Chen schreef:
 
E

Eric Sosman

Mockey Chen wrote On 07/27/06 10:06,:
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "

int main()
{
printf(LOG_PREFIX "some thing.\n");

return 0;
}

I complie and got error:
% cc func.c
func_test.c: In function `main':
func_test.c:6: error: syntax error before "__FUNCTION__"

Probably because __FUNCTION__ has not been defined.
From the usage, it appears you expect __FUNCTION__ to be
a macro that expands to a string literal (which would then
combine with the literals on each side to make one larger
literal). C does not define any such macro, though, so
you must either (1) define it yourself or (2) use a compiler
that defines __FUNCTION__ automatically as an extra feature
beyond what Standard C provides. (I believe that some gcc
versions, if invoked in some modes, will do this.)
I want to use LOG_PREFIX macro automatice expand.
Is there any way to do it?

If you can find a compiler that supports the C99 version
of the Standard, you can use the predefined identifier __func__
for a similar purpose. Note, though, that __func__ is not a
macro and does not expand to a string literal; it refers to an
automatically-generated array of const char containing the
function name as a string. That means you can't concatenate
__func__ with its neighbors the way you're trying to do with
__FUNCTION__, but will need to do something more like

printf ("Function: <%s>: some thing.\n", __func__);
 
K

Kenneth Brody

(Top-posting corrected.)
Mockey Chen schreef: [...]
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "

int main()
{
printf(LOG_PREFIX "some thing.\n");

return 0;
}

I complie and got error:
% cc func.c
func_test.c: In function `main':
func_test.c:6: error: syntax error before "__FUNCTION__"

I want to use LOG_PREFIX macro automatice expand.

It is expanded. However, if your compiler doesn't support the
__FUNCTION__ macro (which, as I understand it, is a C99 feature,
which is supported by some C90 compilers), then this will cause
an error, as the expansion becomes:

printf("Current function: <" __FUNCTION__ ">: " "some thing.\n");

As a quick test, add:

#define __FUNCTION__ "foo"


If your compiler doesn't support it, the only option is to do
something else, or get a compiler that does support it.

[...]
doing things your way it won't work, strings can not be added to
eachother in C

String literals can be "added" just fine:

printf("Hello" " world" ".\n");

or

#define LONG_STRING "This is a long string " \
"that won't fit on a " \
"single line."

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
A

Andrew Poelstra

Hello

why so complicated

One macro? How long have you been doing C?

doing things your way it won't work, strings can not be added to
eachother in C
There has to be always memory to hold it!

Hmm. This compiles for me:
#include <stdio.h>
int main (void)
{
puts ("This is a long" "string that I'm" "adding" "together.");
return 0;
}

Also, please type properly. You type once, but the message is read
thousands of times. (So when it takes an extra 3.6 seconds for a
thousand people to understand your message, it had better saved
you an hour.)
better is it to make a error function like this

void error_logger(char *functionName,char *mess)
{
printf("Current function <%s> %s\n",functionName,mess);
}

The original version was fine, style-wise.
and call it like this

error_logger(__FUNCTION__,"something");


this You can put in a macro like this

#define ERROR_LOG(mess) error_logger(__FUNCTION__,mess)

Now you can call it like

ERROR_LOG("someThing");

I hope that now your quetion is anserd

I saw some other posts below yours; it's probably that it has.

Don't top post!
Don't top post!
Don't top post!
 
M

Martin Ambuhl

Mockey said:
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "

int main()
{
printf(LOG_PREFIX "some thing.\n");

return 0;
}

I complie and got error:
% cc func.c
func_test.c: In function `main':
func_test.c:6: error: syntax error before "__FUNCTION__"

I want to use LOG_PREFIX macro automatice expand.
Is there any way to do it?

There are several ways to accomplish this. Start by removing the
nonstandard identifier __FUNCTION__; the standard provided for this use
is __func__. And remember that __func__ (and the non-standard
__FUNCTION__ for many compilers that provide it) are used as if the were
declared as character arrays, not a string literals. Here's one way:

#include <stdio.h>
#define LOG_PREFIX "Current function: <%s>: %s", __func__,

int main()
{
printf(LOG_PREFIX "some thing.\n");
return 0;
}

Obviously there are some advantages to changing this to

#include <stdio.h>
#define LOG(s) do {\
printf("Current function: <%s>: %s", __func__, s);\
} while 0;

int main()
{
LOG("some thing.\n");
return 0;
}
Thanks in advance.

Why? Are you too fucking important to be bothered with us after you get
your answer? Or are you a bill collector trying to imply you are owed a
response,
 
L

lovecreatesbeauty

Mockey said:
I using #define as following:
#include <stdio.h>
#define LOG_PREFIX "Current function: <" __FUNCTION__ ">: "

What is __FUNCTION__ ? Is it mistyping of __func__ ? __func__ is not a
preprocessor. After preprocessing, a macro likes this: #define STH
"AAA" __func__ is still: "AAA" __func__ . The __func__ remains. For
string literal, "AAA" "BBB" is correct, but __func__ is a string
variable, not a string literal, so "AAA" __func__ is a syntax error.
 
T

Tak-Shing Chan

Why? Are you too fucking important to be bothered with us after you get your
answer? Or are you a bill collector trying to imply you are owed a response,

Totally uncalled for, bad netiquette, and off-topic.

Tak-Shing
 
D

Default User

Kenneth said:
(Top-posting corrected.)
It is expanded. However, if your compiler doesn't support the
__FUNCTION__ macro (which, as I understand it, is a C99 feature,
which is supported by some C90 compilers), then this will cause
an error, as the expansion becomes:


The C99 macro is __func__, so the one above is some
implementation-specific extension.




Brian
 
J

jacob navia

Martin said:
Mockey Chen wrote: [snip]
Thanks in advance.


Why? Are you too fucking important to be bothered with us after you get
your answer? Or are you a bill collector trying to imply you are owed a
response,

Please Mr Chen, do not think that all people in this group are like
this person.

You were polite, and you are welcome!

jacob
 
J

John Devereux

jacob navia said:
Martin said:
Mockey Chen wrote: [snip]
Thanks in advance.


Why? Are you too fucking important to be bothered with us after you
get your answer? Or are you a bill collector trying to imply you
are owed a response,

Please Mr Chen, do not think that all people in this group are like
this person.

You were polite, and you are welcome!

Agreed - I have even used "Thanks in advance" myself on occasion. The
intent was to convey gratitude without cluttering up the thread with
otherwise content-free one line "thanks" posts.
 
K

Keith Thompson

Martin Ambuhl said:
Mockey Chen wrote: [...]
Thanks in advance.

Why? Are you too f***ing important to be bothered with us after you
get your answer? Or are you a bill collector trying to imply you are
owed a response,

[edited for content]

Some people find the phrase "Thanks in advance" offensive. I can sort
of see the point; taken literally, it could imply some sort of
unwelcome obligation, or a rebuke to anyone who doesn't offer help.
But it should be obvious that the *intent* of the phrase is simply to
be polite. It's difficult to reach the same conclusion for "Are you
too f***ing important to be bothered with us after you get your
answer?"

Martin, you seem to be insisting on your own standard of politeness
while being extraordinarily rude yourself. Next time someone
innocently writes "Thanks in advance" or something similar, I suggest
you ignore it. If you must flame someone for something so trivial,
please do so in private; I can't speak for everyone, but I strongly
suspect that nobody else is very impressed by your ability to curse.

Grow up.
 
M

Mark McIntyre

Why? Are you too <expletive removed> important to be bothered with us after you get
your answer?

This is uncalled for, and rude. Martin you need to think before you
post this sort of stuff.
Or are you a bill collector trying to imply you are owed a
response,

I write dozens of emails every day which, if they were asking for
something, I generally end in "thanks". This is by definition "in
advance" of when I get the answer, so...


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

Some people find the phrase "Thanks in advance" offensive. I can sort
of see the point; taken literally, it could imply some sort of
unwelcome obligation, or a rebuke to anyone who doesn't offer help.

I fail to see how this can be, by any stretch of the imagination. Its
merely being polite and no different to saying "have a nice day" or
"best regards" or "cheers" or "yours sincerely" which I also doubt
anyone actually means as anything other than a pleasantry.
If you must flame someone for something so trivial,
please do so in private; I can't speak for everyone, but I strongly
suspect that nobody else is very impressed by your ability to curse.

<aol>
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

jacob navia

Mark said:
I fail to see how this can be, by any stretch of the imagination.

Agreed. If we are going to flame somebody because
he/she is polite we are doomed.

jacob
 
J

J. J. Farrell

Quoting order corrected.
Mockey Chen schreef:


why so complicated

What's complicated about it? It's a simple and common way to do this
sort of thing, assuming __FUNCTION__ expands to a string. Mockey's
problem is most likely that __FUNCTION__ is not defined.
just do it like this

#include <stdio.h>

int main ()
{
printf("Current function <%s>\n",__FUNCTION__);
return 0;
}

Why so complicated? Why do at run-time what can be simply and cleanly
done at compile time?
doing things your way it won't work, strings can not be added to
eachother in C

Adjacent strings are automatically concatenated in C.

const char *str = "This " "is" " "
"a str" "ing";

has exactly the same effect as

const char *str = "This is a string";
There has to be always memory to hold it!

Obviously, but what does that have to do with anything?
better is it to make a error function like this

void error_logger(char *functionName,char *mess)
{
printf("Current function <%s> %s\n",functionName,mess);
}

Not at all better - that requires extra function calls and extra work
in printf().
 
J

J. J. Farrell

Martin said:
Why? Are you too fucking important to be bothered with us after you get
your answer? Or are you a bill collector trying to imply you are owed a
response,

That's grossly out of order.

Messages which just say "thanks" to a reply are irritating, in that
readers go to them eager for new words of wisdom and find nothing.
They're little more than "me toos", though much more acceptable since
they at least have a valid social purpose. It's also very difficult to
know when to send a note of thanks. I've seen threads where the OP has
carefully posted a thanks reply to every response in the thread, making
half the messages in the thread of no value to most people. Given the
nature of Usenet, it can be very difficult to work out when to post a
single thanks reply.

On the other hand, a comment at the end of the original posting
offering thanks in advance makes it clear that the poster will be
grateful for any help he gets, and indicates to me that he doesn't
think he is owed a response. It avoids all the issues with content-free
messages cluttering up the thread. For me, it's the most polite way
overall of handling it (perhaps followed up with individual emails if
any posters are particularly helpful).
 
P

Peter Nilsson

Mark said:
I fail to see how this can be, by any stretch of the imagination.

Perhaps you have no imagination. ;-)
Its merely being polite and no different to saying "have a nice day" ...

Ironic choice of phrase because it's one that I hate hearing from
Americans! Usually, because it's delivered in a forced manner
by someone who doesn't mean it but is saying so because it's
company policy to feign empathy in front of clients.

Martin has is manic moments. Perhaps he's just auditioning for the
US version of "Grumpy Old Men". <g>
 
M

Mark McIntyre

Ironic choice of phrase because it's one that I hate hearing from
Americans!

Me too, but I don't generally respond by telling the other party to
fsck off.
Martin has is manic moments. Perhaps he's just auditioning for the
US version of "Grumpy Old Men". <g>

:)

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Mockey Chen said:
"Martin Ambuhl" <[email protected]>
wrote:<[email protected]>... [...]
There are several ways to accomplish this. Start by removing the
nonstandard identifier __FUNCTION__; the standard provided for this
use is __func__. And remember that __func__ (and the non-standard
__FUNCTION__ for many compilers that provide it) are used as if the
were declared as character arrays, not a string literals. Here's one way:
when I use C, there is no C99, only C89 exist, C99 has lots of new feature,
but not every compiler support it. I thing C89 will be exist years.
I known most of compiler support __FUNCTION__, such as gcc, msvc, xlc...
I use gcc, and it also predefine such a marco.

__FUNCTION__ is exactly as non-standard in C89 (which I prefer to call
C90) as it is in C99.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top