There is no format specifier for Boolean in C language. But we can print True/False using conditional operators.
Suppose you have a boolean β
bool abc = true; bool def = false;
Now in order to print true for abc
using printf
, we donβt have any format specifier like int (%d)
or string (%s)
.
bool abc = true; bool def = false; printf(abc ? "true" : "false"); // Output π true printf(def ? "true" : "false") // Output π false
We need to use format specifier of string along with ternary conditional. Like this β
bool abc = true; bool def = false; printf("Value of abc is %s", abc ? "true" : "false"); // Output π Value of abc is true printf("Value of def is %s", def ? "true" : "false") // Output π Value of def is false
We can also use fputs()
function β
bool abc = true; bool def = false; fputs(abc ? "TRUE" : "FALSE", stdout); // Output π TRUE fputs(def ? "TRUE" : "FALSE", stdout); // Output π FALSE