Identify Trap in Signed vs Unsigned Comparison

Code

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

void safe_compare(int8_t a, uint8_t b) {
    int16_t c = (int16_t) b; 
    if ( a < c)
    {
        printf("1");
    }
    else 
    {
        printf("0");
    }

}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

-1 1

Expected Output

1