Thread: what's wrong with this macro?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    17

    Question what's wrong with this macro?

    Code:
    #include <stdio.h>
    #define P( fmt...)    printf( ##fmt)
    #define FP( fmt...)    fprintf( stderr, ##fmt)
    #define PP( fmt...)    printf( "Wen%d\n",##fmt)
    int main(int argc, char *argv[])
    {
        PP(520);
        FP("Xiao %d\n", 520);
        P("Wener%d",520);/* this not work */
    /*
    test2.c:9:1: error: pasting "(" and ""Wener%d"" does not give a valid preprocessing token
    */
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    sorry i made a mistake

    it should be
    #define P( fmt...) printf( fmt)
    /* it works */

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    If you use this
    Code:
    #define P( fmt...)    fprintf( stdout,##fmt)
    it will be ok

    EDIt - sam is too slow
    Last edited by std10093; 08-06-2012 at 05:14 AM.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Why the ##? ## is used to paste parts of a single token, you don't want that, just remove the double ## everywhere in your macros.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong with the macro definition....
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 04-28-2010, 01:26 AM
  2. RGB macro
    By Magos in forum Game Programming
    Replies: 1
    Last Post: 04-10-2006, 03:57 PM
  3. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  4. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  5. Whats' wrong with this windows Macro?
    By Adock in forum Windows Programming
    Replies: 3
    Last Post: 03-21-2002, 02:42 PM

Tags for this Thread