AnsweredAssumed Answered

char to short - unexpected casting behavior

Question asked by lupal on Aug 21, 2012
Latest reply on Aug 22, 2012 by lupal
Hi all,
I use STM32F205xx for my project but I did the same results with STM32VL Discovery kit.
I debug in Atollic TrueSTUDIO 2.3.0 Lite and I tried TrueSTUDIO 3.1. too.

The problem is that code 
  char a = -50;
  short b = a;
does cast negative values correctly. Internal representation of char should be signed and therefore b variable should be -50 and not 206, which is the result.
I checked this in VisualStudio and the result was -50 as expected. Another brand controller does it as expected.
I checked also disassembly listing. The code for other controller uses signed extend of data for this cast. Atollic makes a simple copy without signed extension.

Do I understand something wrong or is it a problem of a compiler?
How to deal with it correctly? Is it about settings of the compiler?

Thank you for your reactions in advance

Outcomes