I do not know why the following program prints "error!" but not
"right!", can anybody has patience to read this little program and
tell me the reason, much thanks!
#include <stdio.h>
struct sizelist {
int sl_size; /* # of blocks */
int sl_no; /* # of the list in this list */
int sl_leafno; /* B-tree leaf number in the volume */
};
void main(void)
{
int i;
i = -1;
if (i < sizeof(struct sizelist))
printf("right!\n");
else
printf("error!\n");
}
-Chen
I think that sizeof will return an *unsigned* int - you are compareing it to
a signed int that will be promoted to an unsigned int (with a very large
value) and so 'i' is *bigger* than sizeof().
Try:
printf("i=%u sizeof=%u", i, sizeof(struct sizelist));
to show what the compiler thinks.
--
Graham
This is not true.
An int is signed, so is a short. A short is guarenteed to be 2 bytes on all
platforms (-32767 to +32768) an int is machine/platform dependant and
usually a word (4 bytes on most larger boxes).
An unsigned int is positive only.
--
Graham
> I do not know why the following program prints "error!" but not
> "right!", can anybody has patience to read this little program and
> tell me the reason, much thanks!
[snip]
> int i;
>
>
> i = -1;
> if (i < sizeof(struct sizelist))
> printf("right!\n");
> else
> printf("error!\n");
I had something like that happen to me once long ago. If memory serves
me, sizeof( ... ) is unsigned, and so i is quietly turned unsigned as
well (and God help you if i is a negative number then).
Torbjörn Andersson
Guaranteed by whom? All ISO C guarantees is that short is *AT LEAST*
16 bits long (meaning a guaranteed range of -32767 to +32767), which
is exactly the same guarantee as it makes for int.
> An unsigned int is positive only.
Almost; it's non-negative. (Pedants insist that zero is neither
negative nor positive.)
-Larry Jones
Oh, now YOU'RE going to start in on me TOO, huh? -- Calvin
>Graham Macrae wrote:
>>
>> An int is signed, so is a short. A short is guarenteed to be 2 bytes on all
>> platforms (-32767 to +32768) an int is machine/platform dependant and
>> usually a word (4 bytes on most larger boxes).
>
>Guaranteed by whom? All ISO C guarantees is that short is *AT LEAST*
>16 bits long (meaning a guaranteed range of -32767 to +32767), which
>is exactly the same guarantee as it makes for int.
Strictly speaking you should had said only that the C standard
guarantees that shorts and ints have a range of at least -32767
to +32767. The number of bits is not specified; on twos
complement machines it can be done in 16 bits and would usually
(always?) include the value -32768.
--
Roger
>I think that sizeof will return an *unsigned* int
Not according to the C standard. sizeof returns an unsigned
integral type called size_t.
>Try:
> printf("i=%u sizeof=%u", i, sizeof(struct sizelist));
And what happens if sizeof returns something larger than an int?
printf("sizeof=%lu", (unsigned long) sizeof(struct
sizelist));
will worth for all values returned by sizeof.
--
Roger
> Hi everyone,
>
> I do not know why the following program prints "error!" but not
> "right!", can anybody has patience to read this little program and
> tell me the reason, much thanks!
>
> #include <stdio.h>
> struct sizelist {
> int sl_size; /* # of blocks */
> int sl_no; /* # of the list in this list */
> int sl_leafno; /* B-tree leaf number in the volume */
> };
>
> void main(void)
> {
> int i;
>
> i = -1;
> if (i < sizeof(struct sizelist))
> printf("right!\n");
> else
> printf("error!\n");
> }
>
> -Chen
Hi,
Please see below.
Alex
#################################################
############## 1. Program #######################
#################################################
#include <stdio.h>
#include <iostream>
struct sizelist {
int sl_size; /* # of blocks */
int sl_no; /* # of the list in this list */
int sl_leafno; /* B-tree leaf number in the volume */
};
int main(void)
{
int iii = -5;
size_t uuu1 = 3; // size_t is insigned
int uuu2 = 3;
//===============================
cout << "Example#1 : iii + uuu1 = " << iii + uuu1 << endl;
// Here operator+ is
// size_t + size_t
// (because uuu1 is the size_t type), so result is the size_t
type
//===============================
cout << "Example#2 : iii + uuu2 = " << iii + uuu2 << endl;
// Here operator+ is
// int + int
// (because iii and uuu2 are int), so result is the int type
//===============================
int i = -1;
// operator sizeof returns the size_t type,
// so here operator< is
// size_t < size_t
if (i < sizeof(struct sizelist)) // i.e. if ((size_t) i <
sizeof(struct sizelist))
cout << "right" << endl;
else
cout << "error!" << endl;
//===============================
cout << "Example#3 : i = " << i << endl;
//===============================
cout << "Example#4 : (size_t) i = " << (size_t) i << endl;
//===============================
// Solution#1
int s = sizeof(struct sizelist);
cout << "Solution#1 : ";
if (i < s)
cout << "right" << endl;
else
cout << "error!" << endl;
//===============================
// Solution#2
cout << "Solution#2 : ";
if (i < (int) sizeof(struct sizelist))
cout << "right" << endl;
else
cout << "error!" << endl;
return 0;
}
#################################################
############## 2. The results of the running ####
#################################################
Example#1 : iii + uuu1 = 4294967294
Example#2 : iii + uuu2 = -2
error!
Example#3 : i = -1
Example#4 : (size_t) i = 4294967295
Solution#1 : right
Solution#2 : right
#################################################
############## 3. Compiler ######################
#################################################
g++ -v : gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
Note! It is useful to compile with option -Wall
g++ -Wall foo.C
#################################################
############## 4. System ########################
#################################################
uname -a : SunOS tibamsun8 5.6 Generic_105181-09 sun4m sparc
SUNW,SPARCstation-5
#################################################
############## END of INFORMATION ###############
#################################################
Unfortunately, C9X seems set to introduce the integral type "long
long" which may be longer than long.
> >Try:
> > printf("i=%u sizeof=%u", i, sizeof(struct sizelist));
>
> And what happens if sizeof returns something larger than an int?
>
> printf("sizeof=%lu", (unsigned long) sizeof(struct
> sizelist));
>
> will worth for all values returned by sizeof.
For C9X, since size_t may be bigger than a long:
printf("sizeof=" PRIuMAX, (uintmax_t) sizeof(struct sizelist));
PRIuMAX and uintmax_t are defined in <inttypes.h>
Dave Wragg