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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:16:29+00:00 2026-06-10T19:16:29+00:00

How can I write a Standard C( C89 or C99 compliant) macro that is

  • 0

How can I write a Standard C( C89 or C99 compliant) macro that is independent of type specifier?

Specifically, I want to write a macro to compare two numbers which can be of signed type or unsigned type, 8-bits, 16-bits, 32-bits, 64-bits.

What I’m looking for, is something like this:

           int c,  a , b;
           uint16_t p, q, r;

                .
                .
                .

           c = min(a, b);

                .
                .
                .

           r = min(p, q);
                .
                .
                .

ie, the same macro ‘min’ can be used to compare two integers – any width, signed or unsigned.

I know that a basic macro that work can be written like this:

          #define min(x, y)   (x) < (y) ? (x) : (y)

But this involves side-effect of evaluating expressions ‘x’ and ‘y’ twice, which I don’t want.

For robustness, expressions must be evaluated once only.

Till now, my approach seems like this:

          #define min(type, x, y)  ( {  \ 
                  type __tmp1 = (x);                      \
                  type __tmp2 = (y);                      \
                  __tmp1 < __tmp2 ? __tmp1 : __tmp2; } )

          int a, b, c;
          uint16_t p, q, r;

                   .
                   .
                   .

          c = min(int, a, b);

The C compiler throws error: “expecting ; found ( “, and many others.

How can I re-write this macro so that is usage take this form :

          <result> = min( <type>, <parm1>, <parm2> );

It seems that ‘min’ macro can be written so that its usage is:

           min( <result>, <type>, <parm1>, <parm2> );  

But it would be nice to have first form.

Update#1 on 2012-09-04:

Thank you guys, all the info provided in your answers/comments gave me a wider perspective of possibilities. Well, I am not using gcc, neither my compiler supports C11 spec, so the only options that seem to be available are:

  1. Use this form of macro:

           // min( <result>, <type>, <parm1>, <parm2> ); 
    
           #define min(result, type, x, y)  {                   \ 
                  type __tmp1 = (x);                      \
                  type __tmp2 = (y);                      \
                  result = __tmp1 < __tmp2 ? __tmp1 : __tmp2; } 
    
            int p, q, r;
                .
                .
                .
    
            min(r, int, p, q);
    
  2. The other approach is :

          inline char min_char(char x, char y)       { return x < y ? x : y; }
          inline short min_short(short x, short y)    { return x < y ? x : y; }
          inline int min_int(int x, int y)          { return x < y ? x : y; }
          inline long min_long(long x, long y)      { return x < y ? x : y; }
    
          inline unsigned char min_uchar(unsigned char x, unsigned char y)    { return x < y ? x : y; }  
          inline unsigned short min_ushort(unsigned short x, unsigned short y) { return x < y ? x : y; }   
          inline unsigned int min_uint(unsigned int x, unsigned int y)       { return x < y ? x : y; }  
          inline unsigned long min_ulong(unsigned long x, unsigned long y)   { return x < y ? x : y; }  
    
    
          #define   min(type, x, y)    min_##type(x, y)
    
    
          int p, q, r;
               .
               .
               .
    
          r = min(int, p, q);
    

Are there any side-effects of 2nd approach, that might yield unexpected results in some cases, especially regarding ‘short’ and ‘char’ types – signed or unsigned.

Any improvements or suggestions are welcome.

EDITED:

How will the user specify the below function:

            'min_uchar'

He/she must write:

             r = min(uchar, p, q);

Is there any other way to write the function ‘uchar’, so that user can simply write:

             r = min(unsigned char, p, q);

In this case I can’t name the function as ‘min_unsigned char’, as it is not allowed in C.
Is there any other way or user will have to specify type as ‘uchar’ instead of ‘unsigned char’ in macro ‘min’.

Update on 2012-09-10:

Thank you all guys for your help. Since I need to accept an answer, I’m accepting Eitan T‘s answer, but I appreciate responses from all of you. Thanks.

  • 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-06-10T19:16:31+00:00Added an answer on June 10, 2026 at 7:16 pm

    Of course your macro can be independent of type specifier, this is one of the things that makes macros so powerful. You can either:

    1. Pass the variable name and the type and let the macro deal with the type.
    2. Pass the variable name without the type and assume that the macro performs legal operations on that variable, regardless of the type.

    Your actual problem is that in the first form you expect your macro to return an expression, but inside it you declare variables and what-not. I believe that because of that, you put the curly braces in the macro, and now it expands to: ({ bla bla }), which would give you the following error message in ISO C99:

    ISO C forbids braced-groups within expressions

    Unfortunately, in ISO C99 you can’t both have an expression AND declare temporary variables inside it. This is what functions are made for.

    So either use the second form that you described, that is pass the “result” variable to the macro:

    #define min(type_, x_, y_, result_)                    \
        do {                                               \
            type_ __tmp1 = (x_);                           \
            type_ __tmp2 = (y_);                           \
            result_ = (__tmp1 < __tmp2 ? __tmp1 : __tmp2); \
        } while(0)
    

    or use an inline function instead.

    P.S:
    Also, this and this are worth reading for good practices when writing C macros.

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

Sidebar

Related Questions

Is there any class in the .NET framework that can read/write standard .ini files:
I can write an assertion message one of two ways. Stating success: assertEquals( objects
Can anyone please tell me how I can write a bash shell script that
Can someone please explain how you can write a url pattern and view that
It is said that by using C/C++, one can write 'native' programs - that
Is there a standard macro to convert a char string literal that is substituted
Is there an xml or some form of a text standard that one can
In standard c++ we can write : int myArray[5] = {12, 54, 95, 1,
I can write my own but I'm guessing this is very standard. Is there
You can write //google.com instead of http://google.com and https://google.com where // is relative to

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.