C Preprocessor problem

Discussion in 'C' started by taklubaba, Nov 24, 2008.

  1. taklubaba

    taklubaba New Member

    Joined:
    Nov 24, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I have a piece of code as pasted below.

    I get warnings like

    test1.c:29:32: warning: pasting "(" and ""ERROR:"" does not give a valid preprocessing token
    test1.c: In function `main':
    test1.c:29: `__MODULE__' undeclared (first use in this function)
    test1.c:29: (Each undeclared identifier is reported only once
    test1.c:29: for each function it appears in.)

    What does warning: pasting "(" and ""ERROR:"" does not give a valid preprocessing token mean?

    And where is __MODULE__ defined?

    Please provide me some solution.
    Thanks,
    Taklu.

    --------------------------------------------------------
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    #define DL1 "%s:"
    #define DR1 ,__MODULE__
    #define DL2 "%d:"
    #define DR2 ,__LINE__
    
    
    void Trace(char *format, ...);
    
    #define Debug(ARGS...) Trace(##ARGS)
    
    
    #define Error(ARGS,...)   \
            Debug("ERROR:"DL1 DL2 ARGS "\n" DR1 DR2, ## __VA_ARGS__)
    
    
    void Trace(char *format, ...){
    	va_list args;
        va_start(args, format);
    	vprintf(format, args);
    	va_end(args);
    	return;
    }
    
    main(){
    	int i = 10;
    	Error("Print from main");
    	return 0;
    }
     
    Last edited by a moderator: Nov 25, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well there's some serious preprocessor abuse going on here...
    You define Debug() as Trace, then Error as Debug, so Error is actually being defined as Trace().
    When you use the ## pasting token, you need to provide two tokens to paste together, so Trace (##ARGS) makes no sense, and neither does DR2, ##__VA_ARGS__ a few lines later.
    So possibly this is old code that worked on older compilers and needs fixing up for new compilers.
    __MODULE__ and __LINE__ are defined by the preprocessor; __MODULE__ is the filename and __LINE__ is the current line number.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice