DEV Community

loading...

Discussion on: Daily Challenge #310 - Boolean to String Conversion

Collapse
bugb profile image
bugb • Edited

My solution that is submitted to Codewar

const char *boolean_to_string(bool b)
{
    return b ? "true" : "false";
}
Enter fullscreen mode Exit fullscreen mode

My another solution that is accepted int and char type.
C using _Generic. feel free to contribute

#define pt() printf("true\n")
#define pf() printf("false\n")
#define cast_to_boolean (x) _Generic( (x), char: x == '\0' ? pf() : pt(), int: x == 0 ? pf() : pt(), default: pf())
Enter fullscreen mode Exit fullscreen mode