Functions as a single Macro

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • praveenkumarm
    New Member
    • Oct 2008
    • 5

    #1

    Functions as a single Macro

    I want to convert two functions into a single macro in which the sprintf() will have varying arguments and the second function will have the fixed length of arguments

    for eg:

    1. sprintf(buff,"T he numbers are %u,%d, The text is %s", x,y,ch);
    func((unsigned int *)buff,strlen(b uff), z);

    2. sprintf(buff,"h ello");
    func((unsigned int *)buff,strlen(b uff), z);
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You want to substitute sprintf with a macro?
    or you are looking for something else?


    Raghu

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Are you wondering how to write a macro that has a variable number of arguments? The answer varies:

      C89: can't do it.
      C99: Yes! It can be done.
      Gnu C: Nonportable extension allows for this.
      C++: <I don't know>

      Does your flavor of C/C++ support inline functions? Can you use that feature rather than a macro?

      Comment

      • praveenkumarm
        New Member
        • Oct 2008
        • 5

        #4
        Originally posted by gpraghuram
        You want to substitute sprintf with a macro?
        or you are looking for something else?


        Raghu
        I have two functions which i shoud replace with a macro the first function is sprintf and the second function called func which uses the parameter buff from sprintf

        where the sprintf has the varying number of arguments

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Originally posted by praveenkumarm
          I have two functions which i shoud replace with a macro the first function is sprintf and the second function called func which uses the parameter buff from sprintf

          where the sprintf has the varying number of arguments


          void LogThis(const char *fmt, ...)
          You can use #define sprintf LogThis and i think you can do it.

          Raghu

          Comment

          • praveenkumarm
            New Member
            • Oct 2008
            • 5

            #6
            Originally posted by gpraghuram
            void LogThis(const char *fmt, ...)
            You can use #define sprintf LogThis and i think you can do it.

            Raghu

            Can u put me a single macro for the following two function and what are all the declarations i can made before it

            sprintf(buff,"h ello");
            func((unsigned int *)buff,strlen(b uff), z);

            Comment

            • praveenkumarm
              New Member
              • Oct 2008
              • 5

              #7
              Originally posted by praveenkumarm
              Can u put me a single macro for the following two function and what are all the declarations i can made before it

              sprintf(buff,"h ello");
              func((unsigned int *)buff,strlen(b uff), z);

              Hi Raghuram

              ok i think i am not getting your answer

              give me a macro to convert the following part

              #ifdef LABEL

              sprintf(buffer, "--------------------",............. ...);
              function((int *)buffer,strlen (buffer),variab le);

              #endif

              ----------------- indicates a string containing varying data types like %u, %d etc
              ............... .. indicates the variable for whose values the string should take

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by donbock
                C89: can't do it.
                Not entirely true, there is a hack that goes like this

                Code:
                #define PRINT(x) printf x
                
                
                PRINT(("This is a %s", "Hack"));
                PRINT(("The answer is %d, in hex %x", 42, 42));
                Note the double set of parenthesis, this causes all the parameters inside to be treated as the single parameter x. The parenthesis become part of the parameter x which can be verified by using the alternative definition of

                #define PRINT(x) printf( #x "\n" )


                I am not sure that is useful in this situation and I would tend to try an avoid using it anyway but the hack exists.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by Banfa
                  Not entirely true, there is a hack that goes like this

                  Code:
                  #define PRINT(x) printf x
                  
                  
                  PRINT(("This is a %s", "Hack"));
                  PRINT(("The answer is %d, in hex %x", 42, 42));
                  Note the double set of parenthesis, this causes all the parameters inside to be treated as the single parameter x. The parenthesis become part of the parameter x which can be verified by using the alternative definition of

                  #define PRINT(x) printf( #x "\n" )


                  I am not sure that is useful in this situation and I would tend to try an avoid using it anyway but the hack exists.
                  The doubled parentheses work for calling one function, but not so well for a macro that wants to expand into two function calls with each function getting different parameters

                  Comment

                  Working...