Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Printing the range s of unsigned char and unsigned int.

Reply
Thread Tools

Printing the range s of unsigned char and unsigned int.

 
 
pete
Guest
Posts: n/a
 
      09-13-2007
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
 
Reply With Quote
 
 
 
 
user923005
Guest
Posts: n/a
 
      09-13-2007
On Sep 11, 10:50 pm, "Junmin H." <tien...@gmail.com> 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;
}

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-13-2007
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.
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      09-13-2007
On Sep 13, 12:01 pm, "Junmin H." <tien...@gmail.com> 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);

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-13-2007
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
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-13-2007
"Old Wolf" <> a écrit dans le message de news:
m...
> On Sep 13, 12:01 pm, "Junmin H." <tien...@gmail.com> 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.


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-13-2007
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.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-14-2007
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
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-19-2007
"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.


 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-19-2007
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; [...]"

--

 
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
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Padding bits and char, unsigned char, signed char Ioannis Vranos C Programming 6 03-29-2008 10:55 AM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



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