Printing the range s of unsigned char and unsigned int.

C

Charlie Gordon

Eric Sosman said:
Charlie Gordon wrote On 09/19/07 09:07,:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top