All submissions

Identify Trap in Signed vs Unsigned Comparison

Approach & Code

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

/*
int8_t (-127 to 128)
uint8_t (0 to 255)

comparision logic: a < b

Edge cases:
- real trap is the comparision itself not the value assignment

Plan:
- if a < 0 , then return 1
- otherwise compare it
*/

int safe_compare(int8_t a, uint8_t b) {
    // Your logic here
    if(a < 0) return 1;
    else
    {
        if(a < b) return 1;
    }
    return 0;
}

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

Solving Approach

 

 

 

Loading...

Input

-1 1

Expected Output

1