How do you represent a boolean as a %_ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you represent a boolean as a %_

eg for int its %d, String %s

18th May 2022, 9:30 AM
Omar Juma
Omar Juma - avatar
4 Answers
+ 2
bool is 1 or 0, but maybe you can fake it? printf("%s", x?"true":"false") or create a macro: #define fBool(b)((b)?"true":"false") x= 3<5; printf("%s", fBool(x))
18th May 2022, 9:37 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li Thanks for the help👏
18th May 2022, 10:51 AM
Omar Juma
Omar Juma - avatar
+ 1
#include <stdio.h> #include <stdbool.h> int main(void) { bool x = true; /* equivalent to bool x = 1; */ bool y = false; /* equivalent to bool y = 0; */ if (x) /* Functionally equivalent to if (x != 0) or if (x != false) */ { puts("This will print!"); } if (!y) /* Functionally equivalent to if (y == 0) or if (y == false) */ { puts("This will also print!"); } }
19th May 2022, 10:28 AM
Julien Eymard G. KANDJI
Julien Eymard G. KANDJI - avatar
0
/* c++ have std::boolalpha , std::noboolapha*/ #include<iostream> using namespace std; int main() { cout<<"when boolalpha flag is off\n"; cout<<"true: "<<true<<endl; cout<<"false: "<<false<<endl; cout<<"when boolalpha flag is on\n"; cout<<boolalpha<<"true: "<<true<<endl; cout<<boolalpha<<"false: "<<false<<endl; cout<<noboolalpha; }
18th May 2022, 9:50 AM
Bob_Li
Bob_Li - avatar