How can #define printf like function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin

    #1

    How can #define printf like function?

    Hi every one.
    I want to use marco '#define' define a function like printf that have
    variable arguments, do it possible?
    it may be like that, I write a function
    foo(int LineNo, char * fmt, ...)
    I want define
    #define MyFoo(fmt, ...) foo(__LINE__, fmt, ...)
    but it can't works.

    Any help will be appreciated.

  • Greg Comeau

    #2
    Re: How can #define printf like function?

    In article <1106874326.082 583.81470@c13g2 000cwb.googlegr oups.com>,
    Kevin <kevinsc@126.co m> wrote:[color=blue]
    >Hi every one.
    >I want to use marco '#define' define a function like printf that have
    >variable arguments, do it possible?
    >it may be like that, I write a function
    >foo(int LineNo, char * fmt, ...)
    >I want define
    >#define MyFoo(fmt, ...) foo(__LINE__, fmt, ...)
    >but it can't works.[/color]

    The direct way requires a C99 compiler. Very brief description at

    --
    Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

    Comment

    • Jens

      #3
      Re: How can #define printf like function?

      Kevin wrote:[color=blue]
      > Hi every one.
      > I want to use marco '#define' define a function like printf that have
      > variable arguments, do it possible?[/color]

      I'm using gcc 2.95. and this is my debug function:


      #define ERR(n, a...) deb_err( __LINE__, __FILE__, __FUNCTION__,n , ## a )
      void deb_err( int line, const char* file, const char *function, const
      char *format, ... );

      note the ", ## a" the macro even works if no variable argument list is
      given.the precompile will remove the comma ",".

      btw. you could even convert arguments to strings and print them.
      #define ASERR(n,f,a...) do { if(!(n)) ERR( "ASSERT:"#n"\n" #f, ##a ); }
      while(0)

      the conversion is done with "#n"
      e.g.: ASERR( x==0, "x should be zero but it is x=%d", x )

      Comment

      • Richard Bos

        #4
        Re: How can #define printf like function?

        Jens <jens.harms@inf ormatik.uni-oldenburg.de> wrote:
        [color=blue]
        > Kevin wrote:[color=green]
        > > Hi every one.
        > > I want to use marco '#define' define a function like printf that have
        > > variable arguments, do it possible?[/color]
        >
        > I'm using gcc 2.95. and this is my debug function:
        >
        > #define ERR(n, a...) deb_err( __LINE__, __FILE__, __FUNCTION__,n , ## a )[/color]

        That's very pretty, but it isn't C. IIRC it's correct Ganuck, but ISO C
        does not provide it. C99 _does_ provide a different syntax for what the
        OP wants (see Greg's post), but C89 doesn't provide any solution. Yours
        is a syntax error in any standard version of C.

        Richard

        Comment

        • Greg Comeau

          #5
          Re: How can #define printf like function?

          In article <ctd725$t6f$1@n ewssrv2.hrz.uni-oldenburg.de>,
          Jens <jens.harms@inf ormatik.uni-oldenburg.de> wrote:[color=blue]
          >Kevin wrote:[color=green]
          >> Hi every one.
          >> I want to use marco '#define' define a function like printf that have
          >> variable arguments, do it possible?[/color]
          >
          >I'm using gcc 2.95. and this is my debug function:
          >
          >#define ERR(n, a...) deb_err( __LINE__, __FILE__, __FUNCTION__,n , ## a )
          >void deb_err( int line, const char* file, const char *function, const
          >char *format, ... );
          >
          >note the ", ## a" the macro even works if no variable argument list is
          >given.the precompile will remove the comma ",".
          >
          >btw. you could even convert arguments to strings and print them.
          >#define ASERR(n,f,a...) do { if(!(n)) ERR( "ASSERT:"#n"\n" #f, ##a ); }
          >while(0)
          >
          >the conversion is done with "#n"
          >e.g.: ASERR( x==0, "x should be zero but it is x=%d", x )[/color]

          That will work, but requires gcc, or rather a compiler
          which supports gcc's extended variadic macros
          (which obviously would be gcc/g++, and also Comeau C/Comeau C++ in
          GNU C/GNU C++ mode respectively).

          As in my other post, there is a C99 way, which also obviously
          requires a C99 compiler, or at least a compiler supporting
          C99 variadic macros (which at least gcc and Comeau also support).

          In C89, there is no direct way to accomplish this, and requires
          some inventiveness, if even in some cases.
          --
          Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
          Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
          World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
          Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

          Comment

          • Shawn

            #6
            Re: How can #define printf like function?

            Kevin wrote:[color=blue]
            > Hi every one.
            > I want to use marco '#define' define a function like printf that have
            > variable arguments, do it possible?
            > it may be like that, I write a function
            > foo(int LineNo, char * fmt, ...)
            > I want define
            > #define MyFoo(fmt, ...) foo(__LINE__, fmt, ...)
            > but it can't works.
            >
            > Any help will be appreciated.
            >[/color]

            You can use this technique to write macros that take variable number of
            arguments.

            #define MyFoo(a) foo a
            #define MyPrintf(a) printf a

            To use these macros, use two parenthesis instead of just one.

            MyFoo((__LINE__ , "blah %d", 25));
            MyPrintf(("This is a test: %d", 25));


            - Shawn

            Comment

            Working...