Printing the range s of unsigned char and unsigned int.

Discussion in 'C Programming' started by Junmin H., Sep 12, 2007.

  1. Junmin H.

    Junmin H. Guest

    Hello, I am trying to print the range of unsigned char and unsigned int.
    The one for char is working good, gives me a correct output,
    however the other one for int doesnt, why?? Thanks

    #include <stdio.h>

    main(){
    unsigned char c, temp;
    c = temp = 0;
    printf("the range of unsigned char is from %d to ", c);
    while(c >= temp){
    temp = c;
    ++c;
    }
    printf("%d\n", temp);
    return 0;
    }

    OUTPUT: the range of unsigned char is from 0 to 255





    #include <stdio.h>

    main(){
    unsigned int c, temp;
    c = temp = 0;
    printf("the range of unsigned int is from %d to ", c);
    while(c >= temp){
    temp = c;
    ++c;
    }
    printf("%d\n", temp);
    return 0;
    }

    OUTPUT: the range of unsigned int is from 0 to -1

    --
    Posted via a free Usenet account from http://www.teranews.com
    Junmin H., Sep 12, 2007
    #1
    1. Advertising

  2. "Junmin H." <> writes:
    > Hello, I am trying to print the range of unsigned char and unsigned int.
    > The one for char is working good, gives me a correct output,
    > however the other one for int doesnt, why?? Thanks

    [snip]
    > #include <stdio.h>
    >
    > main(){
    > unsigned int c, temp;
    > c = temp = 0;
    > printf("the range of unsigned int is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned int is from 0 to -1


    The "%d" format is for signed int. Use "%u".

    Incidentally, you should use 'int main(void)' rather than 'main()'.

    And if your goal is merely to print the range of unsigned int, you can
    use UINT_MAX, defined in <limits.h>.

    --
    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, Sep 12, 2007
    #2
    1. Advertising

  3. Junmin H. wrote:
    > Hello, I am trying to print the range of unsigned char and unsigned int.
    > The one for char is working good, gives me a correct output,
    > however the other one for int doesnt, why?? Thanks


    Because you incorrectly used "%d", the specifier for a signed int, to
    print an unsigned int. As it happens, on your implementation the bit
    pattern for UINT_MAX is interpreted as the bit pattern for the signed
    int -1. For examples of using the correct specifier, see below:

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

    int main(void)
    {
    printf("[output]\n"
    "the range of unsigned char is from 0 to %u\n"
    "the range of unsigned int is from 0 to %u\n", UCHAR_MAX,
    UINT_MAX);
    return 0;
    }

    [output]
    the range of unsigned char is from 0 to 255
    the range of unsigned int is from 0 to 4294967295

    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned char c, temp;
    > c = temp = 0;
    > printf("the range of unsigned char is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned char is from 0 to 255
    >
    >
    >
    >
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned int c, temp;
    > c = temp = 0;
    > printf("the range of unsigned int is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned int is from 0 to -1
    >
    Martin Ambuhl, Sep 12, 2007
    #3
  4. Junmin H.

    CBFalconer Guest

    "Junmin H." wrote:
    >
    > Hello, I am trying to print the range of unsigned char and unsigned
    > int. The one for char is working good, gives me a correct output,
    > however the other one for int doesnt, why?? Thanks
    >

    .... snip char version ...
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned int c, temp;
    > c = temp = 0;
    > printf("the range of unsigned int is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned int is from 0 to -1


    You have failed to correctly type the printf argument. The
    following works fine here, and is a just barely modified version of
    your code to use a short and avoid an interminable delay. It
    outputs 65535. Note that the maximum value of an unsigned int is
    not normally expressible as an int, and attempting to do so
    involves undefined behaviour.

    #include <stdio.h>

    int main(void) {
    unsigned short c, temp;

    c = temp = 0;
    printf("the range of unsigned int is from %d to ", (int)c);
    while (c >= temp) {
    temp = c;
    ++c;
    }
    printf("%d\n", (int)temp);
    return 0;
    }

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 12, 2007
    #4
  5. CBFalconer said:

    > "Junmin H." wrote:
    >>

    <snip>
    >> printf("%d\n", temp);
    >> return 0;
    >> }
    >>
    >> OUTPUT: the range of unsigned int is from 0 to -1

    >
    > You have failed to correctly type the printf argument. The
    > following works fine here, and is a just barely modified version of
    > your code to use a short and avoid an interminable delay.


    A type change and a cast, Chuck? Hardly "barely modified", when you
    compare it to the minimum change, which is to change a couple of %d to
    %u.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 12, 2007
    #5
  6. Junmin H.

    CBFalconer Guest

    Richard Heathfield wrote:
    > CBFalconer said:
    >> "Junmin H." wrote:
    >>>

    > <snip>
    >>> printf("%d\n", temp);
    >>> return 0;
    >>> }
    >>>
    >>> OUTPUT: the range of unsigned int is from 0 to -1

    >>
    >> You have failed to correctly type the printf argument. The
    >> following works fine here, and is a just barely modified version
    >> of your code to use a short and avoid an interminable delay.

    >
    > A type change and a cast, Chuck? Hardly "barely modified", when
    > you compare it to the minimum change, which is to change a couple
    > of %d to %u.


    But that wouldn't avoid the ~65536 times longer run times for
    checking, which is why I switched to short.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Sep 12, 2007
    #6
  7. CBFalconer said:

    > Richard Heathfield wrote:
    >> CBFalconer said:
    >>> "Junmin H." wrote:
    >>>>

    >> <snip>
    >>>> printf("%d\n", temp);
    >>>> return 0;
    >>>> }
    >>>>
    >>>> OUTPUT: the range of unsigned int is from 0 to -1
    >>>
    >>> You have failed to correctly type the printf argument. The
    >>> following works fine here, and is a just barely modified version
    >>> of your code to use a short and avoid an interminable delay.

    >>
    >> A type change and a cast, Chuck? Hardly "barely modified", when
    >> you compare it to the minimum change, which is to change a couple
    >> of %d to %u.

    >
    > But that wouldn't avoid the ~65536 times longer run times for
    > checking, which is why I switched to short.


    Oops, missed that. Sorry. Still, %hu would have been an improvement over
    a cast.

    Here's another optimisation, btw:

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

    int main(void)
    {
    unsigned int min = 0;
    unsigned int max = UINT_MAX;
    printf("The range of unsigned int is from %u to %u\n",
    min, max);
    return 0;
    }

    This is, of course, O(1).

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 12, 2007
    #7
  8. Junmin H.

    Junmin H. Guest

    Junmin H. wrote:
    > Hello, I am trying to print the range of unsigned char and unsigned int.
    > The one for char is working good, gives me a correct output,
    > however the other one for int doesnt, why?? Thanks
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned char c, temp;
    > c = temp = 0;
    > printf("the range of unsigned char is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned char is from 0 to 255
    >
    >
    >
    >
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned int c, temp;
    > c = temp = 0;
    > printf("the range of unsigned int is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned int is from 0 to -1
    >


    Thank you very much all you guys!

    --
    Posted via a free Usenet account from http://www.teranews.com
    Junmin H., Sep 12, 2007
    #8
  9. "Junmin H." <> a écrit dans le message de news:
    46e7722e$0$24006$...
    > Hello, I am trying to print the range of unsigned char and unsigned int.
    > The one for char is working good, gives me a correct output,
    > however the other one for int doesnt, why?? Thanks
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned char c, temp;
    > c = temp = 0;
    > printf("the range of unsigned char is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned char is from 0 to 255


    A much simpler version, that works in C99 and before :

    replace the while loop with

    temp = c - 1;

    and you are done.

    > #include <stdio.h>
    >
    > main(){
    > unsigned int c, temp;
    > c = temp = 0;
    > printf("the range of unsigned int is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned int is from 0 to -1


    temp = c - 1; works here too, and is much more efficient, O(1) obviously ;-)

    it actually works for any unsigned type.

    --
    Chqrlie.
    Charlie Gordon, Sep 12, 2007
    #9
  10. Junmin H.

    Junmin H. Guest

    On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:

    > "Junmin H." <> a écrit dans le message de news:
    > 46e7722e$0$24006$...
    >> Hello, I am trying to print the range of unsigned char and unsigned int.
    >> The one for char is working good, gives me a correct output,
    >> however the other one for int doesnt, why?? Thanks
    >>
    >> #include <stdio.h>
    >>
    >> main(){
    >> unsigned char c, temp;
    >> c = temp = 0;
    >> printf("the range of unsigned char is from %d to ", c);
    >> while(c >= temp){
    >> temp = c;
    >> ++c;
    >> }
    >> printf("%d\n", temp);
    >> return 0;
    >> }
    >>
    >> OUTPUT: the range of unsigned char is from 0 to 255

    >
    > A much simpler version, that works in C99 and before :
    >
    > replace the while loop with
    >
    > temp = c - 1;
    >
    > and you are done.
    >
    >> #include <stdio.h>
    >>
    >> main(){
    >> unsigned int c, temp;
    >> c = temp = 0;
    >> printf("the range of unsigned int is from %d to ", c);
    >> while(c >= temp){
    >> temp = c;
    >> ++c;
    >> }
    >> printf("%d\n", temp);
    >> return 0;
    >> }
    >>
    >> OUTPUT: the range of unsigned int is from 0 to -1

    >
    > temp = c - 1; works here too, and is much more efficient, O(1) obviously ;-)
    >
    > it actually works for any unsigned type.


    I'm sorry. I don't understand your point.
    Junmin H., Sep 13, 2007
    #10
  11. Junmin H.

    pete Guest

    Junmin H. wrote:
    >
    > Hello, I am trying to print the range of unsigned char and unsigned int.
    > The one for char is working good, gives me a correct output,
    > however the other one for int doesnt, why?? Thanks
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned char c, temp;
    > c = temp = 0;
    > printf("the range of unsigned char is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned char is from 0 to 255


    In "the default argument promotions",
    unsigned char promotes to int
    if int can cover the range of unsigned char;
    which it can, on most hosted implementations.

    > #include <stdio.h>
    >
    > main(){
    > unsigned int c, temp;
    > c = temp = 0;
    > printf("the range of unsigned int is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    > }
    >
    > OUTPUT: the range of unsigned int is from 0 to -1


    #include <stdio.h>

    int main(void)
    {
    printf("The range of unsigned int is from 0 to %u.\n",
    (unsigned)-1);
    return 0;
    }

    --
    pete
    pete, Sep 13, 2007
    #11
  12. Junmin H.

    user923005 Guest

    On Sep 11, 10:50 pm, "Junmin H." <> wrote:
    > Hello, I am trying to print the range of unsigned char and unsigned int.
    > The one for char is working good, gives me a correct output,
    > however the other one for int doesnt, why?? Thanks
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned char c, temp;
    > c = temp = 0;
    > printf("the range of unsigned char is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    >
    > }
    >
    > OUTPUT: the range of unsigned char is from 0 to 255
    >
    > #include <stdio.h>
    >
    > main(){
    > unsigned int c, temp;
    > c = temp = 0;
    > printf("the range of unsigned int is from %d to ", c);
    > while(c >= temp){
    > temp = c;
    > ++c;
    > }
    > printf("%d\n", temp);
    > return 0;
    >
    > }
    >
    > OUTPUT: the range of unsigned int is from 0 to -1
    >
    > --
    > Posted via a free Usenet account fromhttp://www.teranews.com


    #include <stdio.h>
    #include <limits.h>
    #include <float.h>
    int main(void)
    {
    printf("CHAR_MIN %d\n", (int) CHAR_MIN);
    printf("CHAR_MAX %u\n", (unsigned) CHAR_MAX);
    printf("DBL_DIG %u\n", (unsigned) DBL_DIG);
    printf("DBL_EPSILON %*.*g\n", DBL_DIG + 3, DBL_DIG,
    DBL_EPSILON);
    printf("DBL_MANT_DIG %u\n", (unsigned) DBL_MANT_DIG);
    printf("DBL_MAX %*.*g\n", DBL_DIG + 3, DBL_DIG, DBL_MAX);
    printf("DBL_MAX_10_EXP %u\n", (unsigned) DBL_MAX_10_EXP);
    printf("DBL_MAX_EXP %u\n", (unsigned) DBL_MAX_EXP);
    printf("DBL_MIN %*.*g\n", DBL_DIG + 3, DBL_DIG, DBL_MIN);
    printf("DBL_MIN_10_EXP %d\n", DBL_MIN_10_EXP);
    printf("DBL_MIN_EXP %d\n", DBL_MIN_EXP);
    #ifdef DBL_RADIX
    printf("DBL_RADIX %u\n", (unsigned) DBL_RADIX);
    #endif
    #ifdef DBL_ROUNDS
    printf("DBL_ROUNDS %u\n", (unsigned) DBL_ROUNDS);
    #endif
    printf("FLT_DIG %u\n", (unsigned) FLT_DIG);
    printf("FLT_EPSILON %*.*g\n", FLT_DIG + 3, FLT_DIG,
    FLT_EPSILON);
    #ifdef FLT_GUARD
    printf("FLT_GUARD %u\n", (unsigned) FLT_GUARD);
    #endif
    printf("FLT_MANT_DIG %u\n", (unsigned) FLT_MANT_DIG);
    printf("FLT_MAX %*.*g\n", FLT_DIG + 3, FLT_DIG, FLT_MAX);
    printf("FLT_MAX_10_EXP %u\n", (unsigned) FLT_MAX_10_EXP);
    printf("FLT_MAX_EXP %u\n", (unsigned) FLT_MAX_EXP);
    printf("FLT_MIN %*.*g\n", FLT_DIG + 3, FLT_DIG, FLT_MIN);
    printf("FLT_MIN_10_EXP %d\n", FLT_MIN_10_EXP);
    printf("FLT_MIN_EXP %d\n", FLT_MIN_EXP);
    printf("INT_MAX %d\n", INT_MAX);
    printf("INT_MIN %d\n", INT_MIN);
    printf("LDBL_DIG %u\n", (unsigned) LDBL_DIG);
    printf("LDBL_EPSILON %*.*Lg\n", LDBL_DIG + 3, LDBL_DIG, (long
    double) LDBL_EPSILON);
    printf("LDBL_MANT_DIG %u\n", (unsigned) LDBL_MANT_DIG);
    printf("LDBL_MAX %*.*Lg\n", LDBL_DIG + 3, LDBL_DIG, (long
    double) LDBL_MAX);
    printf("LDBL_MAX_10_EXP %u\n", (unsigned) LDBL_MAX_10_EXP);
    printf("LDBL_MAX_EXP %u\n", (unsigned) LDBL_MAX_EXP);
    printf("LDBL_MIN %*.*Lg\n", LDBL_DIG + 3, LDBL_DIG, (long
    double) LDBL_MIN);
    printf("LDBL_MIN_10_EXP %d\n", LDBL_MIN_10_EXP);
    printf("LDBL_MIN_EXP %d\n", LDBL_MIN_EXP);
    #ifdef LDBL_RADIX
    printf("LDBL_RADIX %u\n", (unsigned) LDBL_RADIX);
    #endif
    #ifdef LDBL_ROUNDS
    printf("LDBL_ROUNDS %u\n", (unsigned) LDBL_ROUNDS);
    #endif
    #ifdef LLONG_MAX
    printf("LLONG_MAX %lld\n", LLONG_MAX);
    #endif
    #ifdef LLONG_MIN
    printf("LLONG_MIN %lld\n", LLONG_MIN);
    #endif
    printf("LONG_MAX %ld\n", LONG_MAX);
    printf("LONG_MIN %ld\n", LONG_MIN);
    printf("MB_LEN_MAX %u\n", (unsigned) MB_LEN_MAX);
    printf("SCHAR_MAX %u\n", (unsigned) SCHAR_MAX);
    printf("SCHAR_MIN %d\n", (int) SCHAR_MIN);
    printf("SHRT_MAX %hd\n", SHRT_MAX);
    printf("SHRT_MIN %hd\n", SHRT_MIN);
    printf("UCHAR_MAX %u\n", (unsigned) UCHAR_MAX);
    printf("UINT_MAX %u\n", (unsigned) UINT_MAX);
    #ifdef ULLONG_MAX
    printf("ULLONG_MAX %llu\n", (unsigned long long)
    ULLONG_MAX);
    #endif
    printf("ULONG_MAX %lu\n", (unsigned long) ULONG_MAX);
    printf("USHRT_MAX %u\n", (unsigned) USHRT_MAX);
    printf("CHAR_BIT %u\n", (unsigned) CHAR_BIT);

    return 0;
    }
    user923005, Sep 13, 2007
    #12
  13. Richard Heathfield <> writes:

    > CBFalconer said:
    >
    >> Richard Heathfield wrote:
    >>> CBFalconer said:
    >>>> "Junmin H." wrote:
    >>>>>
    >>> <snip>
    >>>>> printf("%d\n", temp);
    >>>>> return 0;
    >>>>> }
    >>>>>
    >>>>> OUTPUT: the range of unsigned int is from 0 to -1
    >>>>
    >>>> You have failed to correctly type the printf argument. The
    >>>> following works fine here, and is a just barely modified version
    >>>> of your code to use a short and avoid an interminable delay.
    >>>
    >>> A type change and a cast, Chuck? Hardly "barely modified", when
    >>> you compare it to the minimum change, which is to change a couple
    >>> of %d to %u.

    >>
    >> But that wouldn't avoid the ~65536 times longer run times for
    >> checking, which is why I switched to short.

    >
    > Oops, missed that. Sorry. Still, %hu would have been an improvement over
    > a cast.
    >
    > Here's another optimisation, btw:
    >
    > #include <limits.h>
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > unsigned int min = 0;
    > unsigned int max = UINT_MAX;
    > printf("The range of unsigned int is from %u to %u\n",
    > min, max);
    > return 0;
    > }
    >
    > This is, of course, O(1).


    As was the original.

    --
    Ben.
    Ben Bacarisse, Sep 13, 2007
    #13
  14. Junmin H.

    Old Wolf Guest

    On Sep 13, 12:01 pm, "Junmin H." <> wrote:
    > On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:
    > >>
    > >> OUTPUT: the range of unsigned int is from 0 to -1

    >
    > > temp = c - 1; works here too, and is much more efficient
    > > it actually works for any unsigned type.

    >
    > I'm sorry. I don't understand your point.


    Do this:
    printf("%u\n", 0u - 1);
    Old Wolf, Sep 13, 2007
    #14
  15. Ben Bacarisse said:

    > Richard Heathfield <> writes:
    >

    <snip>

    >> This is, of course, O(1).

    >
    > As was the original.


    No, the original was O(CHAR_BIT * sizeof(unsigned int)). Whilst CHAR_BIT
    and sizeof(unsigned int) are constants *for a given platform*, they are
    not the same on all platforms. If you call that O(1), all O(n)
    algorithms are O(1) once you've decided how much data you've got.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 13, 2007
    #15
  16. "Old Wolf" <> a écrit dans le message de news:
    ...
    > On Sep 13, 12:01 pm, "Junmin H." <> wrote:
    >> On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:
    >> >>
    >> >> OUTPUT: the range of unsigned int is from 0 to -1

    >>
    >> > temp = c - 1; works here too, and is much more efficient
    >> > it actually works for any unsigned type.

    >>
    >> I'm sorry. I don't understand your point.

    >
    > Do this:
    > printf("%u\n", 0u - 1);


    Or even simpler:

    printf("%u\n, -1u);

    --
    Chqrlie.
    Charlie Gordon, Sep 13, 2007
    #16
  17. Richard Heathfield <> writes:

    > Ben Bacarisse said:
    >
    >> Richard Heathfield <> writes:
    >>

    > <snip>
    >
    >>> This is, of course, O(1).

    >>
    >> As was the original.

    >
    > No, the original was O(CHAR_BIT * sizeof(unsigned int)). Whilst CHAR_BIT
    > and sizeof(unsigned int) are constants *for a given platform*, they are
    > not the same on all platforms. If you call that O(1), all O(n)
    > algorithms are O(1) once you've decided how much data you've got.


    I knew you were going to say that!

    You are, if you want, perfectly allowed to think of the program as
    somehow parametrized by the types but then I can't see how you can
    claim that your program is O(1) (think what will have to happen in
    printf and the strings that must be printed).

    Oh, you will say you mean O(1) in the umber of printf calls. Well if
    so, I meant O(1) in the number of main calls and so it goes on.

    --
    Ben.
    Ben Bacarisse, Sep 13, 2007
    #17
  18. Ben Bacarisse said:
    > Richard Heathfield <> writes:
    >> Ben Bacarisse said:
    >>> Richard Heathfield <> writes:
    >>>

    >> <snip>
    >>
    >>>> This is, of course, O(1).
    >>>
    >>> As was the original.

    >>
    >> No, the original was O(CHAR_BIT * sizeof(unsigned int)). Whilst
    >> CHAR_BIT and sizeof(unsigned int) are constants *for a given
    >> platform*, they are not the same on all platforms. If you call that
    >> O(1), all O(n) algorithms are O(1) once you've decided how much data
    >> you've got.

    >
    > I knew you were going to say that!


    Curses! You have defeated my secret weapon of surprise.

    > You are, if you want, perfectly allowed to think of the program as
    > somehow parametrized by the types but then I can't see how you can
    > claim that your program is O(1) (think what will have to happen in
    > printf and the strings that must be printed).


    Yes, you're right - my claim must be modified too. The original was
    O(CHAR_BIT * sizeof(unsigned int)), and mine is O(log(CHAR_BIT *
    sizeof(unsigned int)), not O(1) as hitherto claimed. But I'd have got
    away with it if it hadn't been for those meddling kids...

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Sep 14, 2007
    #18
  19. "Junmin H." <> a écrit dans le message de news:
    ...
    > On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:
    >
    >> "Junmin H." <> a écrit dans le message de news:
    >> 46e7722e$0$24006$...
    >>> Hello, I am trying to print the range of unsigned char and unsigned int.
    >>> The one for char is working good, gives me a correct output,
    >>> however the other one for int doesnt, why?? Thanks
    >>>
    >>> #include <stdio.h>
    >>>
    >>> main(){
    >>> unsigned char c, temp;
    >>> c = temp = 0;
    >>> printf("the range of unsigned char is from %d to ", c);
    >>> while(c >= temp){
    >>> temp = c;
    >>> ++c;
    >>> }
    >>> printf("%d\n", temp);
    >>> return 0;
    >>> }
    >>>
    >>> OUTPUT: the range of unsigned char is from 0 to 255

    >>
    >> A much simpler version, that works in C99 and before :
    >>
    >> replace the while loop with
    >>
    >> temp = c - 1;
    >>
    >> and you are done.
    >>
    >>> #include <stdio.h>
    >>>
    >>> main(){
    >>> unsigned int c, temp;
    >>> c = temp = 0;
    >>> printf("the range of unsigned int is from %d to ", c);
    >>> while(c >= temp){
    >>> temp = c;
    >>> ++c;
    >>> }
    >>> printf("%d\n", temp);
    >>> return 0;
    >>> }
    >>>
    >>> OUTPUT: the range of unsigned int is from 0 to -1

    >>
    >> temp = c - 1; works here too, and is much more efficient, O(1) obviously
    >> ;-)
    >>
    >> it actually works for any unsigned type.

    >
    > I'm sorry. I don't understand your point.
    >


    No loop is needed, the following code is portable to all platforms.

    #include <stdio.h>
    int main(void){
    printf("the range of unsigned char is from 0 to %u\n",
    (unsigned char)-1);
    printf("the range of unsigned short is from 0 to %u\n",
    (unsigned short)-1);
    printf("the range of unsigned int is from 0 to %u\n",
    (unsigned int)-1);
    return 0;
    }

    --
    Chqrlie.
    Charlie Gordon, Sep 19, 2007
    #19
  20. Junmin H.

    Eric Sosman Guest

    Charlie Gordon wrote On 09/19/07 09:07,:
    > "Junmin H." <> a écrit dans le message de news:
    > ...
    >
    >>On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:
    >>
    >>
    >>>"Junmin H." <> a écrit dans le message de news:
    >>>46e7722e$0$24006$...
    >>>
    >>>>Hello, I am trying to print the range of unsigned char and unsigned int.
    >>>>The one for char is working good, gives me a correct output,
    >>>>however the other one for int doesnt, why?? Thanks
    >>>>
    >>>>#include <stdio.h>
    >>>>
    >>>>main(){
    >>>> unsigned char c, temp;
    >>>> c = temp = 0;
    >>>> printf("the range of unsigned char is from %d to ", c);
    >>>> while(c >= temp){
    >>>> temp = c;
    >>>> ++c;
    >>>> }
    >>>> printf("%d\n", temp);
    >>>> return 0;
    >>>>}
    >>>>
    >>>>OUTPUT: the range of unsigned char is from 0 to 255
    >>>
    >>>A much simpler version, that works in C99 and before :
    >>>
    >>>replace the while loop with
    >>>
    >>>temp = c - 1;
    >>>
    >>>and you are done.
    >>>
    >>>
    >>>>#include <stdio.h>
    >>>>
    >>>>main(){
    >>>> unsigned int c, temp;
    >>>> c = temp = 0;
    >>>> printf("the range of unsigned int is from %d to ", c);
    >>>> while(c >= temp){
    >>>> temp = c;
    >>>> ++c;
    >>>> }
    >>>> printf("%d\n", temp);
    >>>> return 0;
    >>>>}
    >>>>
    >>>>OUTPUT: the range of unsigned int is from 0 to -1
    >>>
    >>>temp = c - 1; works here too, and is much more efficient, O(1) obviously
    >>>;-)
    >>>
    >>>it actually works for any unsigned type.

    >>
    >>I'm sorry. I don't understand your point.
    >>

    >
    >
    > No loop is needed, the following code is portable to all platforms.
    >
    > #include <stdio.h>
    > int main(void){
    > printf("the range of unsigned char is from 0 to %u\n",
    > (unsigned char)-1);
    > printf("the range of unsigned short is from 0 to %u\n",
    > (unsigned short)-1);
    > printf("the range of unsigned int is from 0 to %u\n",
    > (unsigned int)-1);
    > return 0;
    > }


    "I ran it on my DeathStation 9000 and demons flew
    out of my nose."

    Suggested fix:

    #include <stdio.h>
    int main(void){
    printf("the range of unsigned char is from 0 to %u\n",
    (unsigned int)(unsigned char)-1);
    printf("the range of unsigned short is from 0 to %u\n",
    (unsigned int)(unsigned short)-1);
    printf("the range of unsigned int is from 0 to %u\n",
    (unsigned int)-1);
    return 0;
    }

    References:

    7.19.6.1p8: "[...] o,u,x,X The unsigned int argument is
    converted [...]"

    7.19.6.1p9: "[...] If any argument is not the correct
    type for the corresponding conversion specification, the
    behavior is undefined."

    6.3.1.1p3: "[...] If an int can represent all values of
    the original type, the value is converted to an int; [...]"

    --
    Eric Sosman, Sep 19, 2007
    #20
    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. Timo Freiberger
    Replies:
    3
    Views:
    919
    Bob Hairgrove
    Oct 30, 2004
  2. Hal Styli
    Replies:
    14
    Views:
    1,605
    Old Wolf
    Jan 20, 2004
  3. er
    Replies:
    6
    Views:
    469
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    389
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    704
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page