Identify Trap in Signed vs Unsigned Comparison

Code

#include<stdio.h>
#include<stdint.h>

int compare(int8_t a,uint8_t b){
    if(a<0){
        return 1;
    }
    return(uint8_t)a<b;
}

int main(){
    int8_t a;
    uint8_t b;
    scanf("%hhd %hhu",&a,&b);
    printf("%d",compare(a,b));
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

-1 1

Expected Output

1