Topic : can't compare with sign

Forum : ST7/STM8

Original Post
Post Information Post
November 19, 2008 - 11:01am
Guest

code is :
char a,b ;
b=0;
a=-1;
if(a>9)
b=1;
the result is b=1;

Replies
Post Information Post
+1
0
-1
November 20, 2008 - 11:53am
Raisonance Support Team

Hello Fuzhike,

The C standard does not specify whether the "char" type is signed or not. In your case, it is unsigned.
So "a = -1" is equivalent to "a = 0xFF", which explains you problem.

Just declare a and b as signed using:

signed char a, b;
b=0;
a=-1;
if(a>9)
  b=1;

I hope this helps,
Bruno