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 7025731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:00:56+00:00 2026-05-28T00:00:56+00:00

Define a macro MAX3 that gives the maximum of three values. This is what

  • 0

Define a macro MAX3 that gives the maximum of three values.

This is what I came up with:

#define MAX3(a,b,c) ( ((a) > (b)) && ((a) > (c)) ) ? (a) : ((b) > (c)) ? (b) : (c)

This is hard to read. Is there a way to write an equivalent macro with if–else statements?

  • 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-28T00:00:57+00:00Added an answer on May 28, 2026 at 12:00 am

    Having an expression-like macro with statements inside is not possible in standard C99 language (because in C99 statements and expressions are deeply different, both syntactically and semantically).

    However, GCC (and some other compilers with extensions inspired by GCC, e.g. clang) provides a nice extension for that : statement expressions (the documentation gives an example related to your question). With that extension you could code something like

     #define MAX3(A,B,C) ({ int a=(A); int b=(B); int c=(C); int m; \
         if (a>b) m=a; else m=b;                                    \
         if (c>m) m=c;                                              \
         m; }) /* bad macro */
    

    however that still won’t work if you use that macro like e.g.

     int badex(int x, int y) { 
        int a= x+y; int m= x*y; int d=x-y; 
        return MAX3(a,m,d);
     }
    

    I hope you see why it won’t work (name clashes e.g. between the a inside badex and the a inside MAX3 macro). So you need a way to have unique names at every invocation of your macro. Again, GCC provides a nice extension for that, the __COUNTER__ macro (expanded to a unique number, counting) used with concatenation in the preprocessor.

    Then you’ll code something like

     #define MAX3(A,B,C) MAX3_COUNTED((A),(B),(C),__COUNTER__)
     #define MAX3_COUNTED(A,B,C,N)  ({                          \
        int a_##N=(A); int b_##N=(B); int c_##N=(C);            \
        int m_##N;                                              \
        if (a_##N>b_##N) m_##N = a_##N; else m_#N = b_##N;      \
        if (c_##N>m_##N) m_##N=c_##N;                           \
      m_##N; }) /* better example */
    

    Then the first invocation of our macro e.g. MAX3(i++,j++,k++) might expand to MAX3_COUNTED((i++),(j++),(k++),1) which gets expanded into something using a_1 b_1 … and the second invocation, e.g. MAX3(a,m,d) expanded as MAX3_COUNTED((a),(m),(d),2) would use a_2 b_2 etc so is better.

    And of course, defining a static inline max3(int a, int b, int c) function is cleaner (in particular because of side effects: your MAX3 macro gives naughty effects and results with a call like MAX3(i++,j++,k++) etc)

    The general lesson about this is that you should when possible avoid macro (preferring inline functions), and when you absolutely need macros take care about name clashes and expansions.

    Using GCC invoked as gcc -C -E shows you the preprocessed form of your program.

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

Sidebar

Related Questions

Is there a way that I can define a macro similar to C/C++ macros
Is there a way to define a macro (or something similar) that would allow
Can you define a macro that accesses a normal variable, but in a read-only
I have a macro that looks like this: #define coutError if (VERBOSITY_SETTING >= VERBOSITY_ERROR)
For example, never define a macro like this: #define DANGER 60 + 2 This
Is there a #define compiler (nvcc) macro of CUDA which I can use? (Like
Can you do something like this with a macro in C ? #define SUPERMACRO(X,Y)
In standard library, I found that namespace std is declared as a macro. #define
Is there a way to concatenate keywords in a macro and get C to
I want to do for example: #define macro(a) foo( _blah_, *(dword*)(&a) ); #define macro(a,b)

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.