macros

G

Gaurav Jain

I am not sure what is wrong with this code
============================
#define cat(x,y) x##y

main()
{
cat(cat(3,1),2);
}
============================
Does it have to do anything with # operator ?


Regards,
Gaurav
 
Z

Zoran Cutura

Gaurav Jain said:
I am not sure what is wrong with this code
============================
#define cat(x,y) x##y

main()
{
cat(cat(3,1),2);
}
============================
Does it have to do anything with # operator ?

No it doesn't! It has to do with the ##-preprocessor-operator.

Your code doesn't do what you want? Tell us what you actually wanted to
do.

Do you expect "312" to be inserted instead of "cat(cat(3,1),2)"?

If so try this:

#define cat_xy(x,y) x##y
#define cat(x,y) cat_xy(x,y)

int main(void)
{
cat( cat(3,1) , 2);
return 0;
}
 
S

S.Tobias

Gaurav Jain said:
I am not sure what is wrong with this code
============================
#define cat(x,y) x##y
main()
{
cat(cat(3,1),2);
}
============================
Does it have to do anything with # operator ?

Arguments in macros that are operands to `#' and `##' are not expanded.
If the result of `##' is not a valid preprocessing token, the UB is invoked.

The expansion of the first `cat' gives:
cat(3,1) ## 2
(which is not further expanded), and concatenating `)' and `2'
does not yield a pp-token.

You probably want:
#define xcat(x,y) x ## y
#define cat(x,y) xcat(x,y)
 
R

Ravi Uday

Gaurav said:
I am not sure what is wrong with this code
============================
#define cat(x,y) x##y

main()
{
cat(cat(3,1),2);
}
============================
Does it have to do anything with # operator ?


Regards,
Gaurav

Those '##' are called 'stringizing' preprocessor operators
See -

C-FAQ: 11.17 @
http://www.eskimo.com/~scs/C-faq/top.html

- Ravi
 

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,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top