How to use # & ## operator in C??

G

gu ming

Hi Guys,
Do you know how to use # & ## operator in C?

For example, what's meaning for following code?

#define macro(a) #a
 
R

Richard Heathfield

gu ming said:
Hi Guys,
Do you know how to use # & ## operator in C?

For example, what's meaning for following code?

#define macro(a) #a

"The C Programming Language", 2nd edition, by Kernighan and Ritchie, page
90, from about half-way down the page, right down to the bottom.
 
B

BubbaGump

Hi Guys,
Do you know how to use # & ## operator in C?

For example, what's meaning for following code?

#define macro(a) #a


# can be used for turning an identifier into a string and ## can be
used to create an identifier from a macro's arguments, although I find
## hurts more than it helps. Once a set of code get's large enough,
## starts to make it harder to understand because the names of the
generated variables and functions can no longer be searched for as
easily. They're hidden behind macros.


#include <stdio.h>


#define PRINT_INT(x) \
do { printf("value of %s: %d\n", #x, x); } while(0)

#define CALL_FUNC(x) do { Func##x(); } while(0)


void FuncRead()
{
printf("Entered FuncRead\n");
}


void FuncWrite()
{
printf("Entered FuncWrite\n");
}


int main()
{
int my_variable = 57;

PRINT_INT(my_variable);
CALL_FUNC(Read);
CALL_FUNC(Write);

return 0;
}
 

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,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top