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:
-
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); -
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.
Of course your macro can be independent of type specifier, this is one of the things that makes macros so powerful. You can either:
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: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:
or use an inline function instead.
P.S:
Also, this and this are worth reading for good practices when writing C macros.