Thread: Defining a function as macro - C

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    135

    Defining a function as macro - C

    Hey all.

    I have some definition of MACRO in C which is not compiled.

    Code:
    #define SWAP(a, b) int tmp = a; a = b; b = tmp
    I have seen the warnings/errors of the compiler saying the arguments are not declared, expected expression before int, etc...

    But I don't understand what is the intrinsic difference between this one and other similar MACROS like:

    Code:
    #define MAX(a, b) ((a)>(b)? (a) : (b))
    Does someone can explain it?

    Thanks in advance.
    Last edited by HelpMeC; 12-01-2019 at 12:32 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,556
    Well, both are function-style macros. The former has the problem in that it is trying to do a generic swap, but because it has an int as the type of the temporary, the swap isn't all that generic: but it's possible to include the type of the temporary as a macro argument. It's also multi-statement, in which a better style would be to wrap the statements in a do while (0) loop so that the macro as a whole would expand to just one (compound) statement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    866
    The difference is that the second one consists of an expression whereas your version consists of multiple statements. To properly deal with a macro with multiple statements you should use the following trick:
    Code:
    #define SWAP(a, b) do{ int tmp = a; a = b; b = tmp; }while(0)
    The do loop will only execute the code once, but it's presence allows the multiple statements to appear like a single statement.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    135
    Quote Originally Posted by laserlight View Post
    Well, both are function-style macros. The former has the problem in that it is trying to do a generic swap, but because it has an int as the type of the temporary, the swap isn't all that generic: but it's possible to include the type of the temporary as a macro argument. It's also multi-statement, in which a better style would be to wrap the statements in a do while (0) loop so that the macro as a whole would expand to just one (compound) statement.
    Quote Originally Posted by john.c View Post
    The difference is that the second one consists of an expression whereas your version consists of multiple statements. To properly deal with a macro with multiple statements you should use the following trick:
    Code:
    #define SWAP(a, b) do{ int tmp = a; a = b; b = tmp; }while(0)
    The do loop will only execute the code once, but it's presence allows the multiple statements to appear like a single statement.
    Amazing!
    Thank you guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. defining a macro to replace a function
    By zatlas1 in forum C Programming
    Replies: 3
    Last Post: 12-10-2017, 04:15 PM
  2. Defining and using a macro
    By champen in forum C Programming
    Replies: 5
    Last Post: 01-04-2015, 08:00 PM
  3. Defining a function
    By Zach Sisk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2012, 09:06 AM
  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