Julian V. Noble said:
He can check it [the widths of integer types] with
#include <stdio.h>
int main( void )
{
printf("The number of bytes in an int is %d.\n",sizeof(int));
printf("The number of bytes in a long int is %d.\n",sizeof(long));
return 0;
}
I have used a machine on which the output would be
The number of bytes in an int is 0.
The number of bytes in a long int is 0.
Quiz question 1: The predicted output is nonsense, so
something is clearly wrong. Is the implementation buggy,
or is there an error in the code?
There is an error in the code.
Quiz question 2: How should the code be changed, either
to work around the implementation bug or to correct its
own error?
Try
printf("The number of bytes in an int is %u.\n",
(unsigned)sizeof(int));
printf("The number of bytes in a long int is %u.\n",
(unsigned)sizeof(long));
Bonus question: Once the bug is worked around or cured,
what would the output be? (Admittedly, I have not provided
enough information to answer with certainty. Still, it's
possible to make some pretty shrewd guesses.)
Hmmm. A tricky one.
My first thought is that sizeof() represents some other unsigned integral value
other than that which can be represented in an unsigned int. This means that
sizeof evaluates to either
a) an unsigned char,
b) an unsigned short,
c) an unsigned long, or
d) an unsigned long long
Since integral promotion is required in variadic functions, an unsigned char and
an unsigned short would result in the printf() function obtaining an unsigned
int, and thus would produce the proper results. Since the proper results were
not produced, this means that sizeof evaluates to either an unsigned long or an
unsigned long long. Not many platforms that I know of do this.
So, we have a platform that evaluates sizeof into a long or long long. Since
that's unusual, I'd have to say that the platform is unusual. The few unusual
conforming platforms discussed around here are mostly 'embedded' systems,
usually DSPs and the like. IIRC, the common value discussed here is 64bits for
DSP words (I know it can differ, this is what I recollect appearing /here/, in
comp.lang.c). These sorts of embedded systems seem to either use CHAR_BITS=8, or
CHAR_BITS = wordsize.
Assuming CHAR_BITS == 8, then the likely output would be
The number of bytes in an int is 8.
The number of bytes in a long int is 8.
Assuming CHAR_BITS == wordsize, then the likely output would be
The number of bytes in an int is 1.
The number of bytes in a long int is 1.
--
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed are my own, not my employers')