#define macro with multiple parrams

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JohnZ
    New Member
    • Aug 2008
    • 4

    #define macro with multiple parrams

    Hi,
    I'm trying to define a macro that calls a log function:

    #define LOG(level, device, pcString, args ...) if (level < SERIAL_LOG_LEVE L) _log(device, pcString, ## args)

    void _log (unsigned char device, const char *pcString, ...);

    I want the macro to accept multiple args with "..." but the compiler didn't like it. It works fine if i simply do.

    #define LOG(level, device, pcString, args ) if (level < SERIAL_LOG_LEVE L) _log(device, pcString, ## args)

    and when i call

    d = 0;

    LOG(1, 0, "hello world %d", d);

    What am i doing wrong?

    Thank you for your help.

    Regards,

    John
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I think you miss the, after the args in the #define.

    Raghu

    Comment

    • JohnZ
      New Member
      • Aug 2008
      • 4

      #3
      Originally posted by gpraghuram
      I think you miss the, after the args in the #define.

      Raghu
      Ok, that works with 1 or more arguments, but if i do

      LOG(1, 0, "hello world");

      compiler says too few argument in macro.

      I'm using IAR by the way.

      Thank you.

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by JohnZ
        Ok, that works with 1 or more arguments, but if i do

        LOG(1, 0, "hello world");

        compiler says too few argument in macro.

        I'm using IAR by the way.

        Thank you.

        Since you have specified 4 args followed by elipsis it is expecting 4 arguments atleast.
        Then you shuld change the Macro....


        Raghu

        Comment

        • JohnZ
          New Member
          • Aug 2008
          • 4

          #5
          Originally posted by gpraghuram
          Since you have specified 4 args followed by elipsis it is expecting 4 arguments atleast.
          Then you shuld change the Macro....


          Raghu
          Shouldn't the "..." allow 0 or more arguments? so that the below should accept 3 arguments or more.

          #define LOG(level, device, pcString, ...) \
          if (level < SERIAL_LOG_LEVE L) _log(device, pcString, ## __VA_ARGS__)

          Shouldn't it?

          Thank you.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by JohnZ
            Shouldn't the "..." allow 0 or more arguments? so that the below should accept 3 arguments or more.

            #define LOG(level, device, pcString, ...) \
            if (level < SERIAL_LOG_LEVE L) _log(device, pcString, ## __VA_ARGS__)

            Shouldn't it?

            Thank you.
            This is what the C99 Standard says about it:

            Originally posted by C99
            [#12] If there is a ... in the identifier-list in the macro
            definition, then the trailing arguments, including any
            separating comma preprocessing tokens, are merged to form a
            single item: the variable arguments. The number of arguments
            so combined is such that, following merger, the number of
            arguments is one more than the number of parameters in the
            macro definition (excluding the ...).
            kind regards,

            Jos

            Comment

            • JohnZ
              New Member
              • Aug 2008
              • 4

              #7
              Originally posted by JosAH
              This is what the C99 Standard says about it:



              kind regards,

              Jos
              Thank you very much, I understand what I'm doing wrong now.

              Comment

              Working...