Thread: defining a macro to replace a function

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    29

    defining a macro to replace a function

    Hi all
    I came from a different language where you could have a macro like this:

    mac_func1 macro &a, &b
    some definitions...

    which when you do
    rc = mac_func1 parm1, parm2

    will translate to
    rc = func1 (1, parm1, parm2)
    -------
    in C I can do

    #define mac_func1 (parm1, parm2) func1 (1, parm1, parm2)

    however, if you do
    rc = mac_func1 (myparm1, myparm2)

    The compiler will yell about parm1 and parm2 not defined

    Is there a way in C to virtualize the signature?
    Thanks all
    Ze'ev Atlas

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    777
    Remove the space before the macro's parameter list.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    812
    Usually it is recommended to post a small program that we can compile and test before answering your questions.

    If you are compiling for a 32 or 64 bit O/S such as Windows or Linux, it is usually recommended to use normal functions rather than "macro functions". You really don't gain any noticeable efficiency over a normal function.

    Macro functions are more error-prone! No type checking! Possible side effects. Possible code bloat. etc...

    I no longer use them, or inline functions as well. Current compilers such as gcc or clang will do better optimizations, than you can get using macro functions.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    29
    Thank you all
    Indeed I've left a space which was wrong
    I will take the advice and will not use macro function as I realized that this could lead to errors as stated.
    ZA

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining and using a macro
    By champen in forum C Programming
    Replies: 5
    Last Post: 01-04-2015, 08:00 PM
  2. Defining a function
    By Zach Sisk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2012, 09:06 AM
  3. Problem with defining function
    By curious in forum C Programming
    Replies: 17
    Last Post: 06-01-2009, 10:05 PM
  4. Help! Defining a Function
    By assaf2b in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2008, 09:07 AM
  5. Replies: 3
    Last Post: 10-25-2007, 09:41 AM

Tags for this Thread