Ralf wrote On 01/20/06 12:52,:
If you want to explicitly state the type of an integer constant, you
basically have two options. Either you use an explicit cast, as in:
(unsigned int) 1234
or you use the UL suffixes, as in:
1234UL
Are there -- in general -- any semantic differences in using casts
versus suffixes?
Yes. `(unsigned int)1234' is not a constant: it is
an expression consisting of the operator `(unsigned int)'
applied to the constant operand `1234'. The type of the
expression's result is `unsigned int' and the value is
one thousand two hundred thirty-four.
`1234UL' is a constant. Its type is `unsigned long'
and its value is one thousand two hundred thirty-four.
(Note that the type of this constant is not the same as
the type of the earlier expression.)
One place this makes an easily-observable difference
is in the preprocessor. Since the preprocessor operates
at an early stage of compilation before types "exist,"
it cannot evaluate cast operators -- since casts are all
about type conversions and there aren't any types yet,
there's really nothing the preprocessor can do with them.
Consider the following code:
#include <stdio.h>
int main(void) {
#if 1234UL > 0
puts ("#1: as expected");
#else
puts ("#1: unbelievable!");
#endif
#if (unsigned int)1234 > 0
puts ("#2: as expected");
#else
puts ("#2: unbelievable!");
#endif
return 0;
}
Without compiling and running it, can you predict the
output? (Warning: I've asked a trick question.)