468,463 Members | 2,026 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,463 developers. It's quick & easy.

__FUNCTION__ in another macro

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
Jul 27 '06 #1
19 16058
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:
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
Jul 27 '06 #2


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__);

--
Er*********@sun.com

Jul 27 '06 #3
(Top-posting corrected.)

mdler wrote:
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 it then compiles, and show the function "<foo>", then that's
the problem.
Is there any way to do it?
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:Th*************@gmail.com>

Jul 27 '06 #4
On 2006-07-27, mdler <ol************@gmail.comwrote:
Hello

why so complicated
One macro? How long have you been doing C?

<manual printf() snipped>
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!
>
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.
Is there any way to do it?

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 2 days
Jul 27 '06 #5
Mockey Chen wrote:
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,

Jul 27 '06 #6

Mockey Chen wrote:
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.
int main()
{
printf(LOG_PREFIX "some thing.\n");
Jul 27 '06 #7
On Thu, 27 Jul 2006, Martin Ambuhl wrote:
Mockey Chen wrote:
>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,
Totally uncalled for, bad netiquette, and off-topic.

Tak-Shing
Jul 27 '06 #8
Kenneth Brody wrote:
(Top-posting corrected.)

mdler wrote:
Mockey Chen schreef:
func_test.c:6: error: syntax error before "__FUNCTION__"
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
Jul 27 '06 #9
Martin Ambuhl wrote:
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
Jul 27 '06 #10
jacob navia <ja***@jacob.remcomp.frwrites:
Martin Ambuhl wrote:
>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.

--

John Devereux
Jul 27 '06 #11
Martin Ambuhl <ma*****@earthlink.netwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 27 '06 #12
On Thu, 27 Jul 2006 11:16:31 -0400, in comp.lang.c , Martin Ambuhl
<ma*****@earthlink.netwrote:
>Mockey Chen wrote:
>I using #define as following:

Thanks in advance.

Why? Are you too <expletive removedimportant 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
Jul 27 '06 #13
On Thu, 27 Jul 2006 20:18:37 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>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
Jul 27 '06 #14
Mark McIntyre wrote:
On Thu, 27 Jul 2006 20:18:37 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:

>>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.
Agreed. If we are going to flame somebody because
he/she is polite we are doomed.

jacob
Jul 27 '06 #15
Quoting order corrected.

mdler wrote:
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.
Is there any way to do it?

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().

Jul 27 '06 #16

Martin Ambuhl wrote:
Mockey Chen wrote:

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,
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).

Jul 27 '06 #17
Mark McIntyre wrote:
Keith Thompson <ks***@mib.orgwrote:
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.
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>

--
Peter

Jul 27 '06 #18
On 27 Jul 2006 16:51:52 -0700, in comp.lang.c , "Peter Nilsson"
<ai***@acay.com.auwrote:
>Mark McIntyre wrote:
>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!
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
Jul 28 '06 #19
"Mockey Chen" <mo*********@google.comwrites:
"Martin Ambuhl" <ma*****@earthlink.net>
wrote:<4i************@individual.net>...
[...]
>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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 29 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Neo | last post: by
1 post views Thread by somenath | last post: by
7 posts views Thread by Ravi | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.