MACRO problem

Discussion in 'C Programming' started by prophet, Aug 17, 2005.

  1. prophet

    prophet Guest

    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?
    prophet, Aug 17, 2005
    #1
    1. Advertising

  2. prophet

    prophet Guest

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

    thanx
    prophet, Aug 17, 2005
    #2
    1. Advertising

  3. 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."
    Emmanuel Delahaye, Aug 17, 2005
    #3
  4. prophet

    Thad Smith Guest

    prophet wrote:
    > 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
    Thad Smith, Aug 18, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dead RAM
    Replies:
    20
    Views:
    1,111
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    579
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    501
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    470
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    459
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page