Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Doubt on unsigned integer

1 view
Skip to first unread message

ms00

unread,
Jul 10, 2002, 8:14:21 AM7/10/02
to
can anyone please explain the behavior of unsigned int on overflow or
when it is accidentally fed a -ve value?

the link http://www.eskimo.com/~scs/cclass/int/sx4ca.html says we have
a guaranteed properties on overflow, but i cant relate it with the
results of the following.

int main(void)
{
unsigned int i;

for(i=-10;i<=-1;i++)
printf("%d,",i);
/* prints -10,-9,...,-1,0,1,2,...still continues to be in the loop.
Why? */
}

int main(void)
{
unsigned int i=5;
i=i-10;
printf("%d",i);
/* prints -5 Why? */
}

thanx a lot,
ms00.

pete

unread,
Jul 10, 2002, 8:33:34 AM7/10/02
to
ms00 wrote:
>
> can anyone please explain the behavior of unsigned int on overflow or
> when it is accidentally fed a -ve value?
>
> the link http://www.eskimo.com/~scs/cclass/int/sx4ca.html says we have
> a guaranteed properties on overflow, but i cant relate it with the
> results of the following.
>
> int main(void)
> {
> unsigned int i;
>
> for(i=-10;i<=-1;i++)
> printf("%d,",i);
> /* prints -10,-9,...,-1,0,1,2,...still continues to be in the loop.
> Why? */
> }

What you have there, is a "signed, unsigned mismatch"
which compilers normally warn about,
unless your compiler warning level is set too low.
(-1) gets promoted to -1u, which is UINT_MAX.

>
> int main(void)
> {
> unsigned int i=5;
> i=i-10;
> printf("%d",i);
> /* prints -5 Why? */
> }

Because your compiler didn't feel like showing that it can
distinguish -5u from -5, in that situation, today, this time.

--
pete

Dave Neary

unread,
Jul 10, 2002, 8:45:51 AM7/10/02
to
On 10 Jul 2002 05:14:21 -0700, ms00 said:
> unsigned int i;
>
> for(i=-10;i<=-1;i++)
> printf("%d,",i);
> /* prints -10,-9,...,-1,0,1,2,...still continues to be in the loop.
> Why? */
> unsigned int i=5;
> i=i-10;
> printf("%d",i);
> /* prints -5 Why? */

This is really horrible stuff...

To start with, the comparison i<=-1 will always fail. But we're
still not in UB territory. That's just why you're getting the
infinite loop.

More worser (sic) is that "printf("%d,"i);" is undefined. %d gets
a signed int off the va_list in vfprintf, and since the argument
is unsigned this is undefined behaviour. What you're seeing in
the printf is (probably) the unsigned int value getting converted
to signed int in printf. Since varargs functions have no type
checking, this is a bad thing to do, and we should send arguments
in which are expected, otherwise we risk problems.

There are two occurrences of that. If you want to print an
unsigned integer using printf, you need to use %o, %u, %x or %X,
that is if you want your unsigned value to print in octal,
decimal, hex or HEX (respectively). You must respect the type
constraints of varargs functions.

Dave.

--
David Neary,
E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html

Eric Amick

unread,
Jul 10, 2002, 9:02:02 AM7/10/02
to

"pete" <pfi...@mindspring.com> wrote in message
news:3D2C29...@mindspring.com...

> ms00 wrote:
> >
> > can anyone please explain the behavior of unsigned int on overflow
or
> > when it is accidentally fed a -ve value?
> >
> > the link http://www.eskimo.com/~scs/cclass/int/sx4ca.html says we
have
> > a guaranteed properties on overflow, but i cant relate it with the
> > results of the following.
> > int main(void)
> > {
> > unsigned int i=5;
> > i=i-10;
> > printf("%d",i);
> > /* prints -5 Why? */
> > }
>
> Because your compiler didn't feel like showing that it can
> distinguish -5u from -5, in that situation, today, this time.

Printing an unsigned value with %d is the real problem. Using %u would
produce the expected value.

--
Eric Amick
Columbia, MD

Richard Heathfield

unread,
Jul 10, 2002, 10:28:41 AM7/10/02
to
ms00 wrote:
>
> can anyone please explain the behavior of unsigned int on overflow or
> when it is accidentally fed a -ve value?

In 6.2.5(9), the Standard says:

"A computation involving unsigned operands can never overflow, because a
result that cannot be represented by the resulting unsigned integer type
is reduced modulo the number that is one greater than the largest value
that can be represented by the resulting type."

Note that "reduced", here, doesn't necessarily mean "given a lower
value"; it simply means that the value is adjusted, as if by constant
addition or subtraction by (biggest_value_representable_in_that_type +
1), until it is within the proper range.

The Standard has wording further on, in 6.3.1.3, which explains this
rather better than I can:

"[...] the value is converted by repeatedly adding or subtracting one
more than the maximum value that can be represented in the new type
until the value is in the range of the new type."

>
> the link http://www.eskimo.com/~scs/cclass/int/sx4ca.html says we have
> a guaranteed properties on overflow, but i cant relate it with the
> results of the following.
>
> int main(void)
> {
> unsigned int i;
>
> for(i=-10;i<=-1;i++)

i <= -1 will always be true, because the -1 is promoted to unsigned int
for the purposes of the comparison. Thus, you are actually comparing i
against UINT_MAX, and i is guaranteed to be less than or equal to this
value.

> printf("%d,",i);

You meant %u for unsigned int.

> /* prints -10,-9,...,-1,0,1,2,...still continues to be in the loop.
> Why? */

The negatives are because you got the format specifier wrong. The
endless loop is because you are comparing an unsigned int with the
maximum value an unsigned int can have. It will always be less than or
equal to that value.

> }
>
> int main(void)
> {
> unsigned int i=5;
> i=i-10;
> printf("%d",i);
> /* prints -5 Why? */

Because you used the wrong format specifier. Use %u for unsigned ints.

--
Richard Heathfield : bin...@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


0 new messages