printf get -1 for an unsigned integer

Discussion in 'C Programming' started by lovecreatesbeauty@gmail.com, Apr 11, 2007.

  1. Guest

    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
    $
    , Apr 11, 2007
    #1
    1. Advertising

  2. Guest

    On 11 Apr, 10:42, ""
    <> 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.
    , Apr 11, 2007
    #2
    1. Advertising

  3. 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.
    Richard Heathfield, Apr 11, 2007
    #3
  4. "" <> 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) <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"
    Keith Thompson, Apr 11, 2007
    #4
  5. Guest

    On Apr 11, 3:04 am, wrote:
    > On 11 Apr, 10:42, ""
    >
    > <> 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
    , Apr 11, 2007
    #5
  6. 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
    > $
    >
    Martin Ambuhl, Apr 11, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    600
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,223
  3. Replies:
    14
    Views:
    2,063
    CBFalconer
    Jun 18, 2005
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    423
  5. pozz
    Replies:
    12
    Views:
    718
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page