473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

# and ## operators

When is it appropriate to use # and ##? I have been told that ## is
not useful at all.


nethlek
Nov 13 '05 #1
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/
Nov 13 '05 #2
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.

Nov 13 '05 #3
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/
Nov 13 '05 #4
mbs
> 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.
Nov 13 '05 #5
On 20 Sep 2003 22:53:14 -0700, ne*****@tokyo.com (Mantorok Redgormor)
wrote in comp.lang.c:
When is it appropriate to use # and ##? I have been told that ## is
not useful at all.


On the other hand, my dental hygienist told me just last week that ##
is very useful.

Perhaps the person who told you this is not useful at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #6
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

Nov 13 '05 #7
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.
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
4
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...
6
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...
6
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...
2
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...
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
0
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..
17
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...
28
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...
18
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
0
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...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.