Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf get -1 for an unsigned integer

Reply
Thread Tools

printf get -1 for an unsigned integer

 
 
lovecreatesbeauty@gmail.com
Guest
Posts: n/a
 
      04-11-2007
why does printf get -1 for an unsigned integer?


#include <stdio.h>

int main(void)
{
unsigned int u = 0;

u--;
if (u < 0)
printf("u < 0\n");
else
printf("u >= 0\n");
printf("u: %d\n", u);
return 0;
}

$ cc a.c
$ ./a.out
u >= 0
u: -1
$

 
Reply With Quote
 
 
 
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      04-11-2007
On 11 Apr, 10:42, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.com> wrote:
> why does printf get -1 for an unsigned integer?


It doesn't know how to interpret what you've given it, expect by the
"mask" you specify.

> #include <stdio.h>
>
> int main(void)
> {
> unsigned int u = 0;
>
> u--;

[snip]
> printf("u: %d\n", u);

[snip]

You lied to printf.

Try reading the manual before posting this sort of nonsense.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      04-11-2007
said:

> why does printf get -1 for an unsigned integer?


Because you tricked printf. You said you were giving it a signed int,
and it believed you.

Use %u, not %d, to print unsigned ints.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-11-2007
"" <> writes:
> why does printf get -1 for an unsigned integer?
>
>
> #include <stdio.h>
>
> int main(void)
> {
> unsigned int u = 0;
>
> u--;
> if (u < 0)
> printf("u < 0\n");
> else
> printf("u >= 0\n");
> printf("u: %d\n", u);
> return 0;
> }
>
> $ cc a.c
> $ ./a.out
> u >= 0
> u: -1
> $


Because you lied to it. "%d" expects an argument of type int; you
gave it an argument of type unsigned int. Try "%u".

(You're probably seeing that behavior because (unsigned int)-1, or
UINT_MAX, has the same representation as (int)-1 in 2's-complement,
but that's not guaranteed.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      04-11-2007
On Apr 11, 3:04 am, mark_blue...@pobox.com wrote:
> On 11 Apr, 10:42, "lovecreatesbea...@gmail.com"
>
> <lovecreatesbea...@gmail.com> wrote:
> > why does printf get -1 for an unsigned integer?

>
> It doesn't know how to interpret what you've given it, expect by the
> "mask" you specify.
>
> > #include <stdio.h>

>
> > int main(void)
> > {
> > unsigned int u = 0;

>
> > u--;

> [snip]
> > printf("u: %d\n", u);

>
> [snip]
>
> You lied to printf.
>
> Try reading the manual before posting this sort of nonsense.


Yes Minister

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-11-2007
wrote:
> why does printf get -1 for an unsigned integer?
>
>
> #include <stdio.h>
>
> int main(void)
> {
> unsigned int u = 0;
>
> u--;
> if (u < 0)


Because u is unsigned, the above condition is never satisfied

> printf("u < 0\n");
> else
> printf("u >= 0\n");
> printf("u: %d\n", u);


Because u is unsigned, printing its value with "%d", specifying a
_signed_ value is an error.

> return 0;
> }
>
> $ cc a.c
> $ ./a.out
> u >= 0
> u: -1
> $
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
conversion of signed integer to unsigned integer junky_fellow@yahoo.co.in C Programming 14 06-18-2005 02:29 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57