473,513 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing the range s of unsigned char and unsigned int.

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

Sep 12 '07 #1
20 3189
"Junmin H." <ti*****@gmail.comwrites:
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) ks***@mib.org <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"
Sep 12 '07 #2
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
Sep 12 '07 #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
.... 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

Sep 12 '07 #4
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
Sep 12 '07 #5
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

Sep 12 '07 #6
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
Sep 12 '07 #7
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

Sep 12 '07 #8
"Junmin H." <ti*****@gmail.coma écrit dans le message de news:
46***********************@free.teranews.com...
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.
Sep 12 '07 #9
On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:
"Junmin H." <ti*****@gmail.coma écrit dans le message de news:
46***********************@free.teranews.com...
>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.

Sep 13 '07 #10
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
Sep 13 '07 #11
On Sep 11, 10:50 pm, "Junmin H." <tien...@gmail.comwrote:
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;
}

Sep 13 '07 #12
Richard Heathfield <rj*@see.sig.invalidwrites:
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.
Sep 13 '07 #13
On Sep 13, 12:01 pm, "Junmin H." <tien...@gmail.comwrote:
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);

Sep 13 '07 #14
Ben Bacarisse said:
Richard Heathfield <rj*@see.sig.invalidwrites:
<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
Sep 13 '07 #15
"Old Wolf" <ol*****@inspire.net.nza écrit dans le message de news:
11*********************@57g2000hsv.googlegroups.co m...
On Sep 13, 12:01 pm, "Junmin H." <tien...@gmail.comwrote:
>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.
Sep 13 '07 #16
Richard Heathfield <rj*@see.sig.invalidwrites:
Ben Bacarisse said:
>Richard Heathfield <rj*@see.sig.invalidwrites:
<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.
Sep 13 '07 #17
Ben Bacarisse said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Ben Bacarisse said:
>>Richard Heathfield <rj*@see.sig.invalidwrites:
<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
Sep 14 '07 #18
"Junmin H." <ti*****@gmail.coma écrit dans le message de news:
pa****************************@gmail.com...
On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:
>"Junmin H." <ti*****@gmail.coma écrit dans le message de news:
46***********************@free.teranews.com...
>>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.
Sep 19 '07 #19
Charlie Gordon wrote On 09/19/07 09:07,:
"Junmin H." <ti*****@gmail.coma écrit dans le message de news:
pa****************************@gmail.com...
>>On Thu, 13 Sep 2007 00:56:59 +0200, Charlie Gordon wrote:

>>>"Junmin H." <ti*****@gmail.coma écrit dans le message de news:
46***********************@free.teranews.com.. .

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; [...]"

--
Er*********@sun.com
Sep 19 '07 #20
"Eric Sosman" <Er*********@sun.coma écrit dans le message de news:
1190215690.131515@news1nwk...
Charlie Gordon wrote On 09/19/07 09:07,:
<snip>
>>
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; [...]"
Jinx! You are absolutely right!
Why did I not test run my code on my DS9K before posting!

--
Chqrlie.
Sep 20 '07 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
7786
by: Steven T. Hatton | last post by:
I'm trying to write a program like hexel. I guess I could fish out the source for hexel and look at that, but for now I'm trying to figure out how I can do with with std::stringstream and std::string. I had something working with std::string. I simply treated it as an STL container, and iterated over its elements. The results were a bit...
7
20017
by: crazy | last post by:
Hi It seems something really simple, but can't figure out. It's not my homework :), was just looking at some C problems on the web and came across this one. why doesn't the following code print anything? #include <stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array)) int array = {1,2,3,4,5,6,7};
6
13805
by: Enrico `Trippo' Porreca | last post by:
Suppose I wanted to print an unsigned char (in hex). Should I say: unsigned char x = 0x12; printf("%X\n", x); or printf("%X\n", (unsigned) x);
57
5592
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p expects type 'void *; but argument 2 has type 'void
48
2352
by: Yahooooooooo | last post by:
no gotos no loops how is it possbile..... Asked in interview ... Regards MA
14
2906
by: abhishekkarnik | last post by:
Hi, I am trying to read an exe file and print it out character by character in hexadecimal format. The file goes something like this in hexadecimal 0x4d 0x5a 0x90 0x00 0x03 .... so on When I read it into my array " two_bytes" which has 16 characters in total, I get each of these values read correctly, but when I attempt printing them out,...
7
12232
by: Spoon | last post by:
Hello everyone, In my code, I use uint16_t (exactly 16-bit-wide unsigned integer type) and I need to print their value in base 10. http://www.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html As far as I understand, the recommended method is: #include <inttypes.h>
5
27840
by: A. Farber | last post by:
Hello, I'm trying to print out a const char* buffer by first printing 16 bytes as hex codes, then printing them again as characters or dots (if they aren't printable) and so on: 20 A5 96 74 00 00 00 00 05 AD 4A 7C D3 FF 70 00 ..t......J|..p. 31 B0 66 7F 9F 3D AC 00 9D FF C2 02 AB 10 28 76 1.f..=........(v 49 9A 5E 7C F7 3D E4 00 35...
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5704
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.