Signed & unsigned types

Discussion in 'C Programming' started by Ido Yehieli, Dec 5, 2007.

  1. Ido Yehieli

    Ido Yehieli Guest

    Hi,
    from what i've read (http://tigcc.ticalc.org/doc/
    keywords.html#short) and unsigned int should normally be in the range
    0 to 65535. However, compiling and running this program:

    #include <stdio.h>

    int main(){
    unsigned int y=1;
    y-=2;
    printf("y=%i\n",y);
    }

    produces "y=-1". shouldn't it be either 65535 or some error?

    -ido.
    Ido Yehieli, Dec 5, 2007
    #1
    1. Advertising

  2. Ido Yehieli

    Chris Dollin Guest

    Ido Yehieli wrote:

    > Hi,
    > from what i've read (http://tigcc.ticalc.org/doc/
    > keywords.html#short) and unsigned int should normally be in the range
    > 0 to 65535.


    No, it should normally be in the range that the implementation specifies.
    Not all implementations have 16-bit [[un]signed] ints.

    > However, compiling and running this program:
    >
    > #include <stdio.h>
    >
    > int main(){
    > unsigned int y=1;
    > y-=2;
    > printf("y=%i\n",y);
    > }
    >
    > produces "y=-1". shouldn't it be either 65535 or some error?


    Shouldn't you use an unsigned format to print an unsigned integer?

    --
    Chris "rhetorical" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Dec 5, 2007
    #2
    1. Advertising

  3. Ido Yehieli

    Mark Bluemel Guest

    Ido Yehieli wrote:
    > Hi,
    > from what i've read (http://tigcc.ticalc.org/doc/
    > keywords.html#short) and unsigned int should normally be in the range
    > 0 to 65535. However, compiling and running this program:
    >
    > #include <stdio.h>
    >
    > int main(){
    > unsigned int y=1;
    > y-=2;
    > printf("y=%i\n",y);


    What does the %i mask in printf() expect?

    What mask should you specify for unsigned data?

    > }
    >
    > produces "y=-1". shouldn't it be either 65535 or some error?



    You lied to printf(), by saying you were providing an "int" argument,
    but actually providing an "unsigned int". You get undefined behaviour.

    Some compilers may give you a warning, but you can't rely on that.
    Mark Bluemel, Dec 5, 2007
    #3
  4. Ido Yehieli

    Ido Yehieli Guest

    On Dec 5, 11:25 am, Chris Dollin <> wrote:
    > Shouldn't you use an unsigned format to print an unsigned integer?


    Oh right... thanks.
    Ido Yehieli, Dec 5, 2007
    #4
  5. Ido Yehieli

    Ido Yehieli Guest

    On Dec 5, 11:31 am, Mark Bluemel <> wrote:
    > Some compilers may give you a warning, but you can't rely on that.


    Do you know of a compiler that does strict type checking?
    So that the code above would produce 2 errors or warnings, one for the
    %i and one for making an unsigned int negative?

    -ido.
    Ido Yehieli, Dec 5, 2007
    #5
  6. Ido Yehieli

    Mark Bluemel Guest

    Ido Yehieli wrote:
    > On Dec 5, 11:31 am, Mark Bluemel <> wrote:
    >> Some compilers may give you a warning, but you can't rely on that.

    >
    > Do you know of a compiler that does strict type checking?
    > So that the code above would produce 2 errors or warnings, one for the
    > %i and one for making an unsigned int negative?


    No I don't. GCC is, to some extent, capable of checking printf usage
    but it didn't complain about your source.
    Mark Bluemel, Dec 5, 2007
    #6
  7. Ido Yehieli <> writes:

    > On Dec 5, 11:31 am, Mark Bluemel <> wrote:
    >> Some compilers may give you a warning, but you can't rely on that.

    >
    > Do you know of a compiler that does strict type checking?
    > So that the code above would produce 2 errors or warnings, one for the
    > %i and one for making an unsigned int negative?


    gcc can warn you about format/argument mismatches (provided the format
    string is not computed at run-time) but the other issue is not a type
    error. Subtracting 2 form an unsigned quantity is a perfectly normal
    and correct operation. If the value wraps round according to C's
    rules for unsigned arithmetic that, too, is quite correct so there is
    no error to detect.

    --
    Ben.
    Ben Bacarisse, Dec 5, 2007
    #7
  8. Ido Yehieli

    Ido Yehieli Guest

    On Dec 5, 12:16 pm, Ben Bacarisse <> wrote:
    > Subtracting 2 form an unsigned quantity is a perfectly normal
    > and correct operation. If the value wraps round according to C's
    > rules for unsigned arithmetic that, too, is quite correct so there is
    > no error to detect.


    Yes, you are right.
    I guess what i should do is to use a signed int and add something
    like:

    assert(int_that_should_be_positive>-1);

    to the lines that change its value.

    -ido.
    Ido Yehieli, Dec 5, 2007
    #8
  9. Ido Yehieli

    CBFalconer Guest

    Ido Yehieli wrote:
    > Mark Bluemel <> wrote:
    >
    >> Some compilers may give you a warning, but you can't rely on that.

    >
    > Do you know of a compiler that does strict type checking?
    > So that the code above would produce 2 errors or warnings, one for
    > the %i and one for making an unsigned int negative?


    What 'code above'? See my sig.

    --
    If you want to post a followup via groups.google.com, ensure
    you quote enough for the article to make sense. Google is only
    an interface to Usenet; it's not Usenet itself. Don't assume
    your readers can, or ever will, see any previous articles.
    More details at: <http://cfaj.freeshell.org/google/>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Dec 5, 2007
    #9
  10. Ido Yehieli <> wrote:
    > I guess what i should do is to use a signed int and
    > add something like:
    >
    > assert(int_that_should_be_positive>-1);


    Not if int_that_should_be_positive is an unsigned int or
    higher. In a comparison of unsigned int and int values,
    the int (in this case -1) will be converted to an unsigned
    int.

    Some compilers will actually warn you that the condition
    may always be false in that particular case!

    If you want to test for non-negative integers, use...

    int_that_should_be_positive >= 0

    Again, you may get a warning that this condition is always
    true for unsigned integer types, but you can ignore that
    warning.

    --
    Peter
    Peter Nilsson, Dec 5, 2007
    #10
  11. Ido Yehieli

    Jack Klein Guest

    On Wed, 05 Dec 2007 10:31:46 +0000, Mark Bluemel
    <> wrote in comp.lang.c:

    > Ido Yehieli wrote:
    > > Hi,
    > > from what i've read (http://tigcc.ticalc.org/doc/
    > > keywords.html#short) and unsigned int should normally be in the range
    > > 0 to 65535. However, compiling and running this program:
    > >
    > > #include <stdio.h>
    > >
    > > int main(){
    > > unsigned int y=1;
    > > y-=2;
    > > printf("y=%i\n",y);

    >
    > What does the %i mask in printf() expect?


    I don't know, what do you mean by mask?

    > What mask should you specify for unsigned data?


    Again, what mask? Most people associate the term "mask" with a
    pattern, hopefully of an unsigned integer type, used in bitwise
    operations.

    The C standard uses the term "conversion specifier". It's ever so
    much better than mask, don't you think?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Dec 6, 2007
    #11
  12. Ido Yehieli

    Mark Bluemel Guest

    Jack Klein wrote:
    > On Wed, 05 Dec 2007 10:31:46 +0000, Mark Bluemel
    > <> wrote in comp.lang.c:
    >
    >> Ido Yehieli wrote:


    >>> printf("y=%i\n",y);

    >> What does the %i mask in printf() expect?

    >
    > I don't know, what do you mean by mask?

    [snip]
    > The C standard uses the term "conversion specifier". It's ever so
    > much better than mask, don't you think?
    >

    OK point taken...
    Mark Bluemel, Dec 6, 2007
    #12
    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. Jason Heyes
    Replies:
    2
    Views:
    585
    Jason Heyes
    Feb 14, 2005
  2. Guest
    Replies:
    0
    Views:
    360
    Guest
    Dec 20, 2006
  3. Ben Finney
    Replies:
    11
    Views:
    482
    Dan Bishop
    Dec 24, 2006
  4. Guest
    Replies:
    1
    Views:
    340
  5. pozz
    Replies:
    12
    Views:
    725
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page