Topic : RC51 - integer promotion off - adds const bytes wrongly

Forum : 8051

Original Post
Post Information Post
October 24, 2007 - 8:36pm
Guest

Compile these with ANSI integer promotion off. In the 3rd function, the compiler ignores (int). This happens only when the cast is on the last byte of the constant expression.

int worksOK(void)
{
return (int)0xFF + 0x0A + 0x0D;
}

int alsoOK(void)
{
return 0xFF + (int)0x0A + 0x0D;
}

int butThisIsBad(void)
{
return 0xFF + 0x0A + (int)0x0D;
}

I've posted a previous problem which occurs with this compiler option. Raisonance' recommendation was to not use the ANSI-integer-off option. This compiler option is useful; it shrinks 8-bit code quite a bit; its my default for new code development. So I hope you address this and other issues particular to this option.

regards Steven Pruzina

Replies
Post Information Post
+1
0
-1
October 25, 2007 - 1:48pm
Raisonance Support Team

Hi Steven,

The Raisonance Compilers follow the ANSI standard for the order of evaluation.

So in your third function what you really feed into the compiler is:
return (0xFF + 0x0A) + (int)0x0D;
(This is because the '+' binary operator groups from left to right.
As you devalidated ANSI promotion, what the compiler does is:
return (char)(0xFF + 0x0A) + (int)0x0D;
And (char)(0xFF + 0x0A) resolves to (char)(0x09).

I hope this helps,
Bruno

+1
0
-1
October 27, 2007 - 4:40am
Guest

Hi Bruno

Thanks for explaining this. There's always more to learn.

regards Steven Pruzina