473,387 Members | 1,379 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,387 software developers and data experts.

application of ## operator in C

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

Aug 21 '07 #1
16 1474
On Aug 21, 6:38 pm, "mail2sandee...@gmail.com"
<mail2sandee...@gmail.comwrote:
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

Aug 21 '07 #2
On Aug 21, 9:56 am, karthikbalaguru <karthikbalagur...@gmail.com>
wrote:
On Aug 21, 6:38 pm, "mail2sandee...@gmail.com"

<mail2sandee...@gmail.comwrote:
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 .
You are thinking of the # operator, the OP was asking about the ##
operator which is used for token pasting.

Robert Gamble

Aug 21 '07 #3
ma************@gmail.com wrote:
hi all

what are the applications / examples that requires concatenation of 2
pre-processor variables.
i.e applications of " ## " operators in C/C++
Try the C FAQ, http://c-faq.com/cpp/index.html is probably relevant
around question 10.20 onwards.
Aug 21 '07 #4
karthikbalaguru wrote:
On Aug 21, 6:38 pm, "mail2sandee...@gmail.com"
<mail2sandee...@gmail.comwrote:
>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.
You've explained the uses of the # operator, not the ## operator.

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

Aug 21 '07 #5
karthikbalaguru <ka***************@gmail.comwrites:
[...]
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) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 21 '07 #6
On Aug 22, 12:16 am, Keith Thompson <ks...@mib.orgwrote:
karthikbalaguru <karthikbalagur...@gmail.comwrites:

[...]
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) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apologies.... I happened to see it as # rather than ##.

Karthik Balaguru

Aug 21 '07 #7
"ma************@gmail.com" wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 21 '07 #8
karthikbalaguru wrote:
Keith Thompson <ks...@mib.orgwrote:
.... snip ...
>>
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) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Apologies.... I happened to see it as # rather than ##.
Last chance before PLONKing - snip all signatures, i.e. all that
follows the "-- " marker line.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 22 '07 #9
CBFalconer wrote:
"ma************@gmail.com" wrote:
>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.
AFAIK, the preprocessor _language_ of C90 and C++ is one and the same,
making the question meaningful.
--Ark
Aug 22 '07 #10
On Aug 22, 9:04 am, Ark Khasin <akha...@macroexpressions.comwrote:
CBFalconer wrote:
"mail2sandee...@gmail.com" wrote:
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.

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

Aug 22 '07 #11
ma************@gmail.com wrote:
>
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!

--
Ian Collins.
Aug 22 '07 #12
Ian Collins wrote:
ma************@gmail.com wrote:
.... snip ...
>
>Note: C/C++ means C OR C++ , since it can also be used in C++.

Then write it that way!
That complicated solution actually solves the perennial problem :-)

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 22 '07 #13
On Aug 22, 12:41 am, Ian Collins <ian-n...@hotmail.comwrote:
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.

Aug 22 '07 #14
Justin Spahr-Summers wrote:
On Aug 22, 12:41 am, Ian Collins <ian-n...@hotmail.comwrote:
>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.
We have to retain "backwards" compatibility with C++!

--
Ian Collins.
Aug 22 '07 #15
In article <5j*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>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 <fooand <(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).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 22 '07 #16
Chris Torek wrote:
In article <5j*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>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 <fooand <(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.

--
Ian Collins.
Aug 22 '07 #17

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

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
0
by: Kasey | last post by:
In my application I don't want the operator to have access to the operating system. Can I call the control panel date/time from inside my application and have the operator change the date/time and...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
9
NeoPa
by: NeoPa | last post by:
In VBA (I expect VB would be similar) controlling another Office Application (Automation) can be done using the following steps : Ensure the Reference (VBA Window / Tools / References) has been...
2
by: pssraju | last post by:
Hi, At present application was built on solaris 9 using sun studio 9 (Sun C++ 5.6) & rouguewave sorce pro 5. We are planning to port the same application onto SuSE Linux 9.5.0 using GCC 3.3.3 & RW...
4
by: =?ISO-8859-15?Q?Albe_V=B0?= | last post by:
In my Application, I need to make a certain graphical refresh, interrogating SqlServer, only if Application has focus (i.e. the title bar is blue). Interrogating .Focused property of various...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.