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.

 
 
Junmin H.
Guest
Posts: n/a
 
      09-12-2007
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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-12-2007
"Junmin H." <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-12-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


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
>

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-12-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
>

.... 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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-12-2007
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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-12-2007
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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-12-2007
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
 
Reply With Quote
 
Junmin H.
Guest
Posts: n/a
 
      09-12-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
>
>
>
>
>
> #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

 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-12-2007
"Junmin H." <(E-Mail Removed)> a écrit dans le message de news:
46e7722e$0$24006$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Junmin H.
Guest
Posts: n/a
 
      09-13-2007
On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:

> "Junmin H." <(E-Mail Removed)> a écrit dans le message de news:
> 46e7722e$0$24006$(E-Mail Removed)...
>> 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.

 
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