When is it appropriate to use # and ##? I have been told that ## is
not useful at all.
nethlek 7 1866
In 'comp.lang.c', ne*****@tokyo.com (Mantorok Redgormor) wrote: When is it appropriate to use # and ##? I have been told that ## is not useful at all.
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)
PRT (foo);
PRT (bar);
PRT (baz);
#undef PRT
--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Emmanuel Delahaye <em**********@noos.fr> wrote: In 'comp.lang.c', ne*****@tokyo.com (Mantorok Redgormor) wrote:
When is it appropriate to use # and ##? I have been told that ## is not useful at all.
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)
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:
#define PRT(a) \
printf ("%4s = %ul", #a, (unsigned long) p-> a)
To the OP: the ## operator is needed whenever you need to create a
single token from multiple tokens - this only ever occurs in macros.
Say you had some code like this:
func_a(s->data_a);
func_b(s->data_b);
func_c(s->data_c);
func_d(s->data_d);
func_e(s->data_e);
and you wanted to use a macro to make it less error-prone. You could do
that like this:
#define DO_THING(x) func_ ## x (s -> data ## x)
DO_THING(a);
DO_THING(b);
DO_THING(c);
DO_THING(d);
DO_THING(e);
- Kevin.
In 'comp.lang.c', Kevin Easton <kevin@-nospam-pcug.org.au> wrote: #define PRT(a) \ printf ("%4s = %ul", #baz, (unsigned long) p-> ## a) I think you mean #a there.
Oops, absolultly. Yet another victim of copy and paste...
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:
True enough, my example was poor.
#define PRT(a) \ printf ("%4s = %ul", #a, (unsigned long) p-> a)
--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
> When is it appropriate to use # and ##?
the # operator helps you harness the power of the c-preprocessor.
suppose you define a macro like this:
#define SHOW_INT( var ) printf( #var"=%d\n", var );
and call it later:
int i=800;
...
SHOW_INT( i );
...
it would expend to
printf( "i""=%d\n", i );
and would print
i=800
to stdout.
Here is my version of SHOW, which I use daily:
#define SHOW( val, fmt ) { fprintf( stderr, "%d "#val"=#fmt"\n",
__LINE__, (val) ); }
this macro prints the what it is about to do to stderr and the does
it:
#define DODBG( cmd ) { fprintf( stderr, "%d doing "#cmd"\n",
__LINE__ ); {cmd;} }
.. I have been told that ## is not useful at all
It is. There's an example in K&R, but the book is not here now.
Michael.
Mantorok Redgormor wrote: When is it appropriate to use # and ##? I have been told that ## is not useful at all.
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)?
--Steve
Steve Zimmerman <st******@sonic.net> wrote: Mantorok Redgormor wrote:
When is it appropriate to use # and ##? I have been told that ## is not useful at all.
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");
You failed to allocate some memory for 'Ramanujan' to point to. 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)?
The abort message, induced by the error above, was printed to stderr.
Regards
Irrwahn
--
My other computer is a abacus. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: greg |
last post by:
Discussion is invited on the following proto-PEP.
-------------------------------------------------------------
PEP ??? - Overloadable Boolean Operators...
|
by: GianGuz |
last post by:
Global new and delete operators can be overloaded to suite particulars
needs. Typically they are overloaded to insert useful debugging/trace
informations. What I would to discuss here concerns the...
|
by: bearophileHUGS |
last post by:
Sometimes I suggest to add things to the language (like adding some set
methods to dicts), but I've seen that I tend to forget the meaning of
six set/frozenset operators:
s & t s &= t
s | t s...
|
by: jas_lx |
last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.
Having done some Win32...
|
by: Steve Summit |
last post by:
-----BEGIN PGP SIGNED MESSAGE-----
It's often explained that the reason for some of the imprecision
in C's definition is so that C can be implemented on different
kinds of machines -- say, those...
|
by: raju |
last post by:
hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
|
by: Syanide |
last post by:
here's a bit info for you fellas:
eg.. you have Square classes u wanna add
public static int operator+ (Square s1, Square s2)
{
// your code here
}
comparision operators..
|
by: Steve R. Hastings |
last post by:
I have been studying Python recently, and I read a comment on one
web page that said something like "the people using Python for heavy math
really wish they could define their own operators". The...
|
by: dspfun |
last post by:
I'm trying to get a good understanding of how unary operators work and
have some questions about the following test snippets.
int *p;
~!&*++p--;
It doesn't compile, why? The problem seems to be...
|
by: Zach |
last post by:
Can someone list the various macro operators and what they mean. Came
across a function macro:
#define max(a, b) ((a)>(b)?(a):(b))
What does "?" amd ":" mean in this statement?
Zach
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |