"Hamish M" <ha********@googlemail.comwrites:
Hi I am interested in opinions on this topic.
I have heard that a suffix is not a good solution and type casts are
much better
for example.
-----------------------------------------------------------------
#define MAX_UWORD (T_UWORD)65535
or
#define MAX_UWORD 65535u
-------------------------------------------------------------------
Where UWORD is unsigned short int.
And how do we know that UWORD is unsigned short int? I believe you
when you say that it is, but it's not obvious to someone reading the
code, and it might be defined as something else in another version of
the program.
A cast lets you specify any integer type you like. A suffix only lets
you specify one of the predefined types.
On the other hand, integer constants are usually implicitly converted
to whatever type is necessary, so it's not usually important for the
constant to be of the exact correct type. It can matter if you're
passing it as an argument to a variadic function, but in that case you
should probably use a cast anyway (on the call, not on the
definition), and you have to keep promotions in mind (you can't
actually pass an unsigned short value to a variadic function).
For an integer constant, the value is usually more important than the
type; the type can be imposed by the context in which it's used.
Finally, if I were going to use a cast in the macro definition, I'd
enclose the whole thing in parentheses. Rather than
#define MAX_UWORD (T_UWORD)65535
I'd write
#define MAX_UWORD ((T_UWORD)65535)
I'm not sure there's any context in which it would matter, but it's
much easier to add the parentheses than to prove they're not
necessary.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.