M
Mantorok Redgormor
When is it appropriate to use # and ##? I have been told that ## is
not useful at all.
nethlek
not useful at all.
nethlek
In said:When is it appropriate to use # and ##? I have been told that ## is
not useful at all.
Emmanuel Delahaye said:I use it every day. For example, it very useful for local debug macros.
Say, I have a structure that represent some configuration data:
typedef struct
{
unsigned int foo;
some_enum_type bar;
unsigned long baz;
}
config_s;
At a certain stage of my code, I want a trace of the values on the console.
Ok, I can write
printf ("foo = %u", p->foo);
printf ("bar = %u", (unsigned int) p->bar);
printf ("baz = %ul", p->baz);
which is rapidly boring is I have 20 elements or more...
What can be done to help and avoid errors is to ask the proprocessor to
write the code for you:
#define PRT(a) \
printf ("%4s = %ul", #baz, (unsigned long) p-> ## a)
In 'comp.lang.c' said:I think you mean #a there.
In the expression p->bar, there are three
tokens: "p", "->" and "bar". So you don't need the ## token pasting
operator here - this definition works:
I have been told that ## is not useful at all
When is it appropriate to use # and ##? I have been told that ## is
not useful at all.
Mantorok said:When is it appropriate to use # and ##? I have been told that ## is
not useful at all.
Steve Zimmerman said:Mantorok,
Thank you for your post. I don't know if ## is useful at all.
The following program uses ##, but not in a very useful way.
#include <stdio.h>
#include <string.h>
#define paste(front, back) front ## back /* p. 92, K&R2 */
int main()
{
char *paste(Rama, nujan); /* char *Ramanujan; */
strcpy(Ramanujan, "Ramanujan is great");
printf("%s\n", paste(Rama, nujan));
return 0;
}
Output of program: Ramanujan is great
Aborted
Why does this program's output contain two streams (stdout and stderr)?
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.