# [newbie]value of a in "unsigned int a=-16"

Discussion in 'C Programming' started by iceBlue, Jul 3, 2003.

1. ### iceBlueGuest

Hello

I have this small doubt on unsigned data.
In the following code snippet:

unsigned int a=-16;
int b=-15;
if(b>a) printf("b>a");
else printf("a>b");

Iam getting b>a as the answer.Why?
With unsigned data,what i thought was that,they dont have to reserve the
first bit for sign.Then how come the value of a is -16 even when its
unsigned?

-iceBlue-
-----------------------------------------------------------------
Life is far too important a thing ever to talk seriously about.

iceBlue, Jul 3, 2003

2. ### Default UserGuest

"Arthur J. O'Dwyer" wrote:
>
> On Thu, 3 Jul 2003, iceBlue wrote:
> >
> > Hello
> >
> > I have this small doubt on unsigned data.

>
> First of all, what you have is a "question." "Doubt" is a verb,
> meaning "to be unsure of the truth of." As in, "I doubt that
> English is your first language."

Is English *your* first language?

Main Entry: 2doubt
Function: noun
Date: 13th century
1 a : uncertainty of belief or opinion that often interferes with
decision-making b : a deliberate suspension of judgment
2 : a state of affairs giving rise to uncertainty, hesitation, or
suspense
3 a : a lack of confidence : DISTRUST b : an inclination not to believe
or accept
synonym see UNCERTAINTY

Brian Rodenborn

Default User, Jul 3, 2003

3. ### Simon BiberGuest

"Arthur J. O'Dwyer" <> wrote:
> Since -16 cannot be represented as an unsigned int, the
> compiler implicitly converts -16 to an unsigned value by
> repeated addition of UINT_MAX. Thus, after the
> initialization, a == UINT_MAX-16.

#include <stdio.h>
#include <limits.h>

int main(void)
{
unsigned a = -16;
printf("UINT_MAX == %u\n", UINT_MAX);
printf("UINT_MAX - 15 == %u\n", UINT_MAX - 15);
printf("UINT_MAX - 16 == %u\n", UINT_MAX - 16);
printf("a == %u\n", a);
return 0;
}

C:\prog\c>gcc -std=c99 -pedantic -Wall -W -O2 unsigned.c -o unsigned
C:\prog\c>unsigned
UINT_MAX == 4294967295
UINT_MAX - 15 == 4294967280
UINT_MAX - 16 == 4294967279
a == 4294967280

The compiler converts -16 to an unsigned value by repeated
addition of UINT_MAX + 1. Thus, after the initialization,
a == UINT_MAX - 15.

> Since a and b are of different types, one of them must be
> promoted to the type of the other one. Since a is unsigned
> and b is not, it is b that gets promoted. So the value
> being compared to UINT_MAX-16 in this expression is
> (unsigned int)b, or UINT_MAX-15.

No, it's comparing `a' which is UINT_MAX-15 to `(unsigned)b'
which is UINT_MAX-14.

--
Simon.

Simon Biber, Jul 4, 2003
4. ### Brett FrankenbergerGuest

In article <>,
Arthur J. O'Dwyer <> wrote:
>
>> unsigned int a=-16;

>
>Since -16 cannot be represented as an unsigned int, the compiler
>implicitly converts -16 to an unsigned value by repeated addition
>of UINT_MAX. Thus, after the initialization, a == UINT_MAX-16.

- Brett

Brett Frankenberger, Jul 4, 2003
5. ### Default UserGuest

Re: [OT] Re: [newbie]value of a in "unsigned int a=-16"

"Arthur J. O'Dwyer" wrote:

> > > First of all, what you have is a "question." "Doubt" is a verb,

> > Is English *your* first language?
> >
> > Main Entry: 2doubt
> > Function: noun

> Yeah, yeah. I didn't bother to actually look up the word just
> to correct the guy's usage. (But its *first* entry in the dictionary
> is as a verb. So.)

Something has to be listed first, that doesn't make secondary listings
incorrect.

My point was that it's a poor idea to try and correct someone's grammar
on usenet. It would have been a fine idea to point out that,
idiomatically, the use of doubt there was not typical English.

Brian Rodenborn

Default User, Jul 7, 2003
6. ### Default UserGuest

Don Porges wrote:
>
> "Mark McIntyre" <> wrote in message news:...
> > On Thu, 3 Jul 2003 14:01:01 -0400 (EDT), in comp.lang.c , "Arthur J.
> > O'Dwyer" <> wrote:

> > Well, actually doubt is a noun too. But it means a disbelief. And I
> > have no doubt that this is not what the OP means.

That's untrue. Doubt can be used as the OP did, it just isn't a
*typical* usage.

> I've noticed this error all over Usenet, all of a sudden -- maybe the last 3 months or so.
> Has some new incorrect English-as-a-second-language textbook come out that's spreading
> this?

It's not an error per se, just atypical. As to its frequency in some of
our non-native English-speaking friends, that I don't have a clue about.
It does seem to be rather common of late.

Brian Rodenborn

Default User, Jul 7, 2003