MACRO problem

P

prophet

I am sorry if this is not the concerned group.I am gettin following
problem while compiling this code:

#include<stdio.h>
#define PRI(item) printf ## (" ## item ## ");
void main()
{
PRI(hello)
}

While compiling i get

a.c:5:1: pasting "printf" and "(" does not give a valid preprocessing
token

What could be the possible problem with the macro defination?
 
P

prophet

sorry for spaming your mailboxes....
i got the answer by searching the archives!

thanx
 
E

Emmanuel Delahaye

prophet wrote on 17/08/05 :
I am sorry if this is not the concerned group.I am gettin following
problem while compiling this code:

#include<stdio.h>
#define PRI(item) printf ## (" ## item ## ");
void main()

main() returns int. Always.
{
PRI(hello)
}

While compiling i get

a.c:5:1: pasting "printf" and "(" does not give a valid preprocessing
token

Sure, the code is not conforming.
What could be the possible problem with the macro defination?

You are making it too complicated (hence wrong).

Try that:

#include <stdio.h>

#define PRI(item) printf("%s\n", #item)

int main (void)
{
PRI (hello world);
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
T

Thad Smith

prophet said:
I am sorry if this is not the concerned group.I am gettin following
problem while compiling this code:

#include<stdio.h>
#define PRI(item) printf ## (" ## item ## ");
void main()
{
PRI(hello)
}

While compiling i get

a.c:5:1: pasting "printf" and "(" does not give a valid preprocessing
token

What could be the possible problem with the macro defination?

The token paste operator is used to paste separate preprocessing tokens
into a single preprocessing token. It is typically used for generating
a single identifier from separate identifier parts. Both printf and (
are preprocessing tokens that become tokens in a later translation
phase. There is simply no preprocessing token consisting of an
identifier preceding a left parenthesis. The C Standard gives the details.

Thad
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top