c - Why do I need to cast value of -1 as a char (when compiled for AVR microcontroller) -


i new embedded c , c. think may misunderstanding basic here , appreciate help.

i have following function outputs "return -1" expect if run on windows (compiler: gnu gcc compiler).

#include <stdio.h> char test();  int main() {     if (test() == -1 )      {         printf("return -1");     }      else      {         printf("return not -1");     }     return 0; }  char test() {     return -1; } 

if compile avr hardware, outputs "return not -1" cannot understand (compiler: gnu gcc compiler, avr-gcc (winavr 20100110) 4.3.3).

if change first line to:

    if (test() == char(-1) ) { 

then outputs "return -1" expected both scenarios.

why need explicitly cast -1 char?

the issue seems whether or not char signed or unsigned, happens implementation specific.

you might able around using signed char wherever need use negative values. furthermore, wise ensure you're using same data types in comparison (assuming test() returns signed char):

if (test() == (signed char)(-1) )  

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -