All submissions

Identify Trap in Signed vs Unsigned Comparison

 

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

int safe_compare(int8_t a, uint8_t b) {
    // Your logic here
        // If a is negative, it's always less than any unsigned value
    if (a < 0)
        return 1;
    
    // If a is non-negative, we can compare
    // Cast a to uint8_t since we know it's positive
    return ((uint8_t)a < b);
}

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

 

 

 

 

Loading...

Input

-1 1

Expected Output

1