Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:15:45+00:00 2026-05-28T19:15:45+00:00

I often see instances in which using a macro is better than using a

  • 0

I often see instances in which using a macro is better than using a function.

Could someone explain me with an example the disadvantage of a macro compared to a function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:15:46+00:00Added an answer on May 28, 2026 at 7:15 pm

    Macros are error-prone because they rely on textual substitution and do not perform type-checking. For example, this macro:

    #define square(a) a * a
    

    works fine when used with an integer:

    square(5) --> 5 * 5 --> 25
    

    but does very strange things when used with expressions:

    square(1 + 2) --> 1 + 2 * 1 + 2 --> 1 + 2 + 2 --> 5
    square(x++) --> x++ * x++ --> increments x twice
    

    Putting parentheses around arguments helps but doesn’t completely eliminate these problems.

    When macros contain multiple statements, you can get in trouble with control-flow constructs:

    #define swap(x, y) t = x; x = y; y = t;
    
    if (x < y) swap(x, y); -->
    if (x < y) t = x; x = y; y = t; --> if (x < y) { t = x; } x = y; y = t;
    

    The usual strategy for fixing this is to put the statements inside a “do { … } while (0)” loop.

    If you have two structures that happen to contain a field with the same name but different semantics, the same macro might work on both, with strange results:

    struct shirt 
    {
        int numButtons;
    };
    
    struct webpage 
    {
        int numButtons;
    };
    
    #define num_button_holes(shirt)  ((shirt).numButtons * 4)
    
    struct webpage page;
    page.numButtons = 2;
    num_button_holes(page) -> 8
    

    Finally, macros can be difficult to debug, producing weird syntax errors or runtime errors that you have to expand to understand (e.g. with gcc -E), because debuggers cannot step through macros, as in this example:

    #define print(x, y)  printf(x y)  /* accidentally forgot comma */
    print("foo %s", "bar") /* prints "foo %sbar" */
    

    Inline functions and constants help to avoid many of these problems with macros, but aren’t always applicable. Where macros are deliberately used to specify polymorphic behavior, unintentional polymorphism may be difficult to avoid. C++ has a number of features such as templates to help create complex polymorphic constructs in a typesafe way without the use of macros; see Stroustrup’s The C++ Programming Language for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was hoping someone could explain to me what bad thing could happen in
I often see that websites use more than one page to display their information,
I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
You often see, on sites like The Daily WTF , examples of overengineered code
I often see something like that: something.property|escape something is an object, property is it's
I often see people saying that certain software is very opinionated or that Microsoft
I often see two conflicting strategies for method interfaces, loosely summarized as follows: //
I often see two styles, INSERT select and insert into select, what are the
I often see people asking XML/XSLT related questions here that root in the inability
I often see code like int hashCode(){ return a^b; } Why XOR?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.