title: C macros independent of type specifier
body: <p>How can I write a Standard C( C89 or C99 compliant) macro that is <em>independent of type specifier</em>?</p>
  
  <p>Specifically, I want to write a macro <em>to compare two numbers which can be of signed type or unsigned type, 8-bits, 16-bits, 32-bits, 64-bits.</em></p>
  
  <p>What I'm looking for, is something like this:</p>
  
  <pre><code>           int c,  a , b;
             uint16_t p, q, r;
  
                  .
                  .
                  .
  
             c = min(a, b);
  
                  .
                  .
                  .
  
             r = min(p, q);
                  .
                  .
                  .
  </code></pre>
  
  <p>ie, <em>the same macro 'min' can be used to compare two integers - any width, signed or unsigned.</em>        </p>
  
  <p>I know that a basic macro that work can be written like this:</p>
  
  <pre><code>          #define min(x, y)   (x) &lt; (y) ? (x) : (y)
  </code></pre>
  
  <p>But this involves <em>side-effect of evaluating expressions 'x' and 'y' twice</em>, which I don't want.</p>
  
  <p>For robustness, expressions must be evaluated once only.</p>
  
  <p>Till now, my approach seems like this:</p>
  
  <pre><code>          #define min(type, x, y)  ( {  \ 
                    type __tmp1 = (x);                      \
                    type __tmp2 = (y);                      \
                    __tmp1 &lt; __tmp2 ? __tmp1 : __tmp2; } )
  
            int a, b, c;
            uint16_t p, q, r;
  
                     .
                     .
                     .
  
            c = min(int, a, b);
  </code></pre>
  
  <p>The C compiler throws error: "expecting ; found ( ", and many others.</p>
  
  <p>How can I re-write this macro so that is <em>usage</em> take this form :</p>
  
  <pre><code>          &lt;result&gt; = min( &lt;type&gt;, &lt;parm1&gt;, &lt;parm2&gt; );
  </code></pre>
  
  <p>It seems that 'min' macro can be written so that its usage is:</p>
  
  <pre><code>           min( &lt;result&gt;, &lt;type&gt;, &lt;parm1&gt;, &lt;parm2&gt; );  
  </code></pre>
  
  <p>But it would be nice to have first form.</p>
  
  <p><strong>Update#1 on 2012-09-04:</strong></p>
  
  <p>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:</p>
  
  <ol>
  <li><p>Use this form of macro:</p>
  
  <pre><code>       // min( &lt;result&gt;, &lt;type&gt;, &lt;parm1&gt;, &lt;parm2&gt; ); 
  
         #define min(result, type, x, y)  {                   \ 
                type __tmp1 = (x);                      \
                type __tmp2 = (y);                      \
                result = __tmp1 &lt; __tmp2 ? __tmp1 : __tmp2; } 
  
          int p, q, r;
              .
              .
              .
  
          min(r, int, p, q);
  </code></pre></li>
  <li><p>The other approach is :</p>
  
  <pre><code>      inline char min_char(char x, char y)       { return x &lt; y ? x : y; }
        inline short min_short(short x, short y)    { return x &lt; y ? x : y; }
        inline int min_int(int x, int y)          { return x &lt; y ? x : y; }
        inline long min_long(long x, long y)      { return x &lt; y ? x : y; }
  
        inline unsigned char min_uchar(unsigned char x, unsigned char y)    { return x &lt; y ? x : y; }  
        inline unsigned short min_ushort(unsigned short x, unsigned short y) { return x &lt; y ? x : y; }   
        inline unsigned int min_uint(unsigned int x, unsigned int y)       { return x &lt; y ? x : y; }  
        inline unsigned long min_ulong(unsigned long x, unsigned long y)   { return x &lt; y ? x : y; }  
  
  
        #define   min(type, x, y)    min_##type(x, y)
  
  
        int p, q, r;
             .
             .
             .
  
        r = min(int, p, q);
  </code></pre></li>
  </ol>
  
  <p>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.</p>
  
  <p>Any improvements or suggestions are welcome.</p>
  
  <p><strong>EDITED:</strong>                </p>
  
  <p>How will the user specify the below function:</p>
  
  <pre><code>            'min_uchar'
  </code></pre>
  
  <p>He/she must write:</p>
  
  <pre><code>             r = min(uchar, p, q);
  </code></pre>
  
  <p>Is there any other way to write the function 'uchar', so that user can simply write:</p>
  
  <pre><code>             r = min(unsigned char, p, q);
  </code></pre>
  
  <p>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'.</p>
  
  <p><strong>Update on 2012-09-10:</strong></p>
  
  <p>Thank you all guys for your help. Since I need to accept an answer, I'm accepting <em>Eitan T</em>'s answer, but I appreciate responses from all of you. Thanks.</p>