-
Casting isn't portable?
I ran the following code below and got 2 different outputs depending on the architecture I ran it on.
Code:
int main()
{
char x = -1;
int y = x;
printf("0x%x\n", y);
return 0;
}
On x86 I got 0xffffffff. Which is exactly what I'd expect. But on powerpc I got 0xff. Is casting really not portable? If so, what is the best way to deal with it?
-
char's are signed on some archs and unsigned on others.
As an experiment try signed char and unsigned char on both archs.
-
I'm not sure what you're getting at with that suggestion. But I did try replacing the char with a short and correctly got 0xffffffff. So I guess that means it is unsigned on the powerpc arch I'm using.
-
Unadorned char can be signed or unsigned the standard does not mandate which. There are compiler switches to force the use of one type though.
-
Replacing the char with a short doesn't prove anything (shorts are always signed unless you specifically request an unsigned short).
I meant to try forcing an unsigned char:
Code:
unsigned char x = -1;
and then forcing a signed char:
Code:
signed char x = -1;
and see the difference.
-
Oh. I didn't even realize there was a signed keyword. I've never seen that used anywhere.