How come << falls through at 16 on a 32-bit integer?
Steve Summit
scs at adam.mit.edu
Sun Apr 14 10:37:24 AEST 1991
In article <11004 at uwm.edu> markh at csd4.csd.uwm.edu (Mark William Hopkins) writes:
> Why am I getting 0 for output here when unsigned long's are 32 bits with the
> Quick C Version 2.5 compiler?
> * unsigned long M = 34;
> * printf("%08x\n", M << 16);
The short answer, which we'll be seeing numerous repetitions of
over the next few days, is that M << 16, being a long, must be
printed with %lx (or, of course, %08lx in this example).
I've noticed that this question has been much more frequent since
ANSI-style function prototypes started coming into widespread
use. Prototypes are not a crutch (though it's easy and tempting
to use them as one); it's important to understand that prototypes
do not take care of the types of all function call arguments
everywhere, and that the programmer must therefore remember to
think about argument type when required. In particular,
prototypes never act on the variable arguments in a variable-
length argument list, such as the arguments following the format
string of a printf-style function.
(It has always been easy to forget that the printf format string
is virtually always interpreted only at run-time, and that the
programmer is therefore responsible for carefully matching
argument types to %-format specifiers. Some versions of lint
will do a static check of printf arguments and format strings, as
long as the format strings are simple compile-time constants, as
they usually are.)
> This is a compiler bug, according to my understanding of the Quick C manual
> and C language.
Not to criticize you, but any time *anyone's* understanding of
the language doesn't match the behavior of the compiler, prompting
a cry of "compiler bug!", it's usually the "understanding" that's
at fault. (Sadly, compilers do occasionally have bugs, so a rule
like "always believe the compiler" must not be applied religiously.
As learn(1) sagely points out, "Occasionally lessons are incorrect...
Such lessons may be skipped with the `skip' command, but it takes
some sophistication to recognize the situation.")
Steve Summit
scs at adam.mit.edu
More information about the Comp.lang.c
mailing list