c - Comparison between unsigned int and int without using cast operator -
everyone looked around there exists topic question yet not find.
unsigned int x = 5; int y = -3; if(y<x) func1(); else func2(); func2 called . want func1 called.
i know must use cast operator when comparing these values.
not allowed use cast operator or changing type of variable.
how can solve problem?
first check if y negative value, knowing that, know x bigger since unsigned.
if y not negative, compare value directly x. not think cause issue since there no negative sign present.
see below example:
if(y<0) { //x>y func1(); } else if (y<x) { //lets y=3, , x=5 func1(); } else { func2(); }
Comments
Post a Comment