application of ## operator in C

M

mail2sandeepnl

hi all

what are the applications / examples that requires concatenation of 2
pre-processor variables.
i.e applications of " ## " operators in C/C++
i think its only use is in Compiler construction, but how i don't
know,
pls any one help me.
bye
sandeep
 
K

karthikbalaguru

hi all

what are the applications / examples that requires concatenation of 2
pre-processor variables.
i.e applications of " ## " operators in C/C++
i think its only use is in Compiler construction, but how i don't
know,
pls any one help me.
bye
sandeep

It is used for the Macro replacement within a String Literal .
It is also called as stringizing operator .
It is used to avoid the warning message like
"warning : macro replacement within string literal"

#define MYPRINTF(var,fmt) \
printf("MYPRINTF Prints "#var" = "#fmt" \n",var)

so invocations like - MYPRINTF(i,%d);
will be expanded to - printf("MYPRINTF Prints i = %d \n",i);

Hope this helps.

Best Regards,
Karthik Balaguru
 
R

Robert Gamble

It is used for the Macro replacement within a String Literal .
It is also called as stringizing operator .

You are thinking of the # operator, the OP was asking about the ##
operator which is used for token pasting.

Robert Gamble
 
S

santosh

karthikbalaguru said:
It is used for the Macro replacement within a String Literal .
It is also called as stringizing operator .
It is used to avoid the warning message like
"warning : macro replacement within string literal"

#define MYPRINTF(var,fmt) \
printf("MYPRINTF Prints "#var" = "#fmt" \n",var)

so invocations like - MYPRINTF(i,%d);
will be expanded to - printf("MYPRINTF Prints i = %d \n",i);

Hope this helps.

You've explained the uses of the # operator, not the ## operator.

To OP: It is used for concatenation of adjacent tokens.
 
K

Keith Thompson

karthikbalaguru said:
It is used to avoid the warning message like
"warning : macro replacement within string literal"

(The OP asked about ##; you answer was about #, but others have
already pointed that out.)

When do you get such a warning? Macros aren't replaced within string
literals, though I think they were in some pre-ANSI versions of the
language.
 
K

karthikbalaguru

[...]
It is used to avoid the warning message like
"warning : macro replacement within string literal"

(The OP asked about ##; you answer was about #, but others have
already pointed that out.)

When do you get such a warning? Macros aren't replaced within string
literals, though I think they were in some pre-ANSI versions of the
language.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Apologies.... I happened to see it as # rather than ##.

Karthik Balaguru
 
C

CBFalconer

what are the applications / examples that requires concatenation
of 2 pre-processor variables. i.e applications of " ## " operators
in C/C++ i think its only use is in Compiler construction, but how
i don't know,

There is no such language as C/C++, making the question
meaningless.
 
C

CBFalconer

karthikbalaguru said:
.... snip ...

Apologies.... I happened to see it as # rather than ##.

Last chance before PLONKing - snip all signatures, i.e. all that
follows the "-- " marker line.
 
A

Ark Khasin

CBFalconer said:
There is no such language as C/C++, making the question
meaningless.
AFAIK, the preprocessor _language_ of C90 and C++ is one and the same,
making the question meaningful.
--Ark
 
M

mail2sandeepnl

AFAIK, the preprocessor _language_ of C90 and C++ is one and the same,
making the question meaningful.
--Ark


I think ## (token pasting operator ) can be used in parsing
configuration files, may in used in compiler designing,
is there any other applications of ## other than these 2?

Note: C/C++ means C OR C++ , since it can also be used in C++.
-sandeep
 
I

Ian Collins

I think ## (token pasting operator ) can be used in parsing
configuration files

How can it? Pasting tokens is a compile time operation, not a run time one.

The are mag case where you may want to post tokens, one example form
some code I'm working on:

void check(char const* fn, int result, char const* file, int line);

#define CheckOk(fn, args) check(#fn, fn##args, __FILE__, __LINE__)

Which passes the function name and the result of its call to the
function check().
Note: C/C++ means C OR C++ , since it can also be used in C++.

Then write it that way!
 
J

Justin Spahr-Summers

The are mag case where you may want to post tokens, one example form
some code I'm working on:

void check(char const* fn, int result, char const* file, int line);

#define CheckOk(fn, args) check(#fn, fn##args, __FILE__, __LINE__)

Which passes the function name and the result of its call to the
function check().

Of course, if you're not afraid to use C99, you could just do

#define CheckOk(fn, ...) check(#fn, fn(__VA_ARGS__), __FILE__,
__LINE__)

which, to me, is a lot easier to understand and use at first glance.
But to each their own, and you may have reservations against C99.
 
I

Ian Collins

Justin said:
Of course, if you're not afraid to use C99, you could just do

#define CheckOk(fn, ...) check(#fn, fn(__VA_ARGS__), __FILE__,
__LINE__)

which, to me, is a lot easier to understand and use at first glance.
But to each their own, and you may have reservations against C99.
We have to retain "backwards" compatibility with C++!
 
C

Chris Torek

The are mag case where you may want to [paste] tokens, one example form
some code I'm working on:

void check(char const* fn, int result, char const* file, int line);

#define CheckOk(fn, args) check(#fn, fn##args, __FILE__, __LINE__)

Which passes the function name and the result of its call to the
function check().

If you mean to use this as, e.g.:

CheckOk(foo, (1, 2.5))

which should pass to check() these four arguments:

- the string "foo" (or more precisely, the address of the 'f' in
this string),
- the result of calling foo(1, 2.5),
- the source file, and
- the source file line number

then the macro should be defined *without* using the token-pasting
operator. Pasting together the two tokens <foo> and <(> results
in the invalid pp-token <foo(>, which gives undefined behavior.
(Thus, it may or may not work, depending on the implementation.)

"Real" examples of "proper" use of token pasting are relatively
rare. One can construct artificial examples easily enough though --
for instance, your CheckOk macro might be used instead to call
two variants of every function name:

#define Check2(fn, args) \
(check(#fn "1", fn ## 1 args, __FILE__, __LINE__), \
check(#fn "2", fn ## 2 args, __FILE__, __LINE__))

The expansion of Check2(foo, (1, 2.5)) is then:

check("foo" "1", foo1 (1, 2.5), "sourcefile.c", 42),
check("foo" "2", foo2 (1, 2.5), "sourcefile.c", 42)

(with the expansion occupying a single line, broken into two
lines only for newsgroup posting purposes).
 
I

Ian Collins

Chris said:
The are mag case where you may want to [paste] tokens, one example form
some code I'm working on:

void check(char const* fn, int result, char const* file, int line);

#define CheckOk(fn, args) check(#fn, fn##args, __FILE__, __LINE__)

Which passes the function name and the result of its call to the
function check().

If you mean to use this as, e.g.:

CheckOk(foo, (1, 2.5))

which should pass to check() these four arguments:

- the string "foo" (or more precisely, the address of the 'f' in
this string),
- the result of calling foo(1, 2.5),
- the source file, and
- the source file line number

then the macro should be defined *without* using the token-pasting
operator. Pasting together the two tokens <foo> and <(> results
in the invalid pp-token <foo(>, which gives undefined behavior.
(Thus, it may or may not work, depending on the implementation.)
Thanks for pointing that out Chris, I'll update the code.
 
K

karthikbalaguru

hi all

what are the applications / examples that requires concatenation of 2
pre-processor variables.
i.e applications of " ## " operators in C/C++
i think its only use is in Compiler construction, but how i don't
know,
pls any one help me.
bye
sandeep

It is often useful to merge two tokens into one while expanding
macros. This is called token pasting or token concatenation. The `##'
preprocessing operator performs token pasting. When a macro is
expanded, the two tokens on either side of each `##' operator are
combined into a single token, which then replaces the `##' and the two
original tokens in the macro expansion. Usually both will be
identifiers, or one will be an identifier and the other a
preprocessing number. When pasted, they make a longer identifier. This
isn't the only valid case. It is also possible to concatenate two
numbers (or a number and a name, such as 1.5 and e3) into a number.
Also, multi-character operators such as += can be formed by token
pasting.

However, two tokens that don't together form a valid token cannot be
pasted together. For example, you cannot concatenate x with + in
either order. If you try, the preprocessor issues a warning and emits
the two tokens. Whether it puts white space between the tokens is
undefined. It is common to find unnecessary uses of `##' in complex
macros. If you get this warning, it is likely that you can simply
remove the `##'.

Reference - http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html - one
of the link that speaks in simple terms.

Karthik Balaguru
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top