Add Signed and Unsigned Integers Safely

Code

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

int16_t signed_unsigned_sum(int8_t a, uint8_t b) {
    // Your logic here

    return ((int)a+b);
}

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

Solving Approach

Typecast to int, it will do the sum properly.

 

Loading...

Input

-10 20

Expected Output

10