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!