What is the printf format specifier for bool?
There is no format specifier for bool
types. However, since any integral type shorter than int
is promoted to int
when passed down to printf()
's variadic arguments, you can use %d
:
bool x = true;
printf("%d\n", x); // prints 1
But why not:
printf(x ? "true" : "false");
or, better:
printf("%s", x ? "true" : "false");
or, even better:
fputs(x ? "true" : "false", stdout);
instead?
C++ printing boolean, what is displayed?
The standard streams have a boolalpha
flag that determines what gets displayed -- when it's false, they'll display as 0
and 1
. When it's true, they'll display as false
and true
.
There's also an std::boolalpha
manipulator to set the flag, so this:
#include <iostream>
#include <iomanip>
int main() {
std::cout<<false<<"\n";
std::cout << std::boolalpha;
std::cout<<false<<"\n";
return 0;
}
...produces output like:
0
false
For what it's worth, the actual word produced when boolalpha
is set to true is localized--that is, <locale>
has a num_put
category that handles numeric conversions, so if you imbue a stream with the right locale, it can/will print out true
and false
as they're represented in that locale. For example,
#include <iostream>
#include <iomanip>
#include <locale>
int main() {
std::cout.imbue(std::locale("fr"));
std::cout << false << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
...and at least in theory (assuming your compiler/standard library accept "fr" as an identifier for "French") it might print out faux
instead of false
. I should add, however, that real support for this is uneven at best--even the Dinkumware/Microsoft library (usually quite good in this respect) prints false
for every language I've checked.
The names that get used are defined in a numpunct
facet though, so if you really want them to print out correctly for particular language, you can create a numpunct
facet to do that. For example, one that (I believe) is at least reasonably accurate for French would look like this:
#include <array>
#include <string>
#include <locale>
#include <ios>
#include <iostream>
class my_fr : public std::numpunct< char > {
protected:
char do_decimal_point() const { return ','; }
char do_thousands_sep() const { return '.'; }
std::string do_grouping() const { return "\3"; }
std::string do_truename() const { return "vrai"; }
std::string do_falsename() const { return "faux"; }
};
int main() {
std::cout.imbue(std::locale(std::locale(), new my_fr));
std::cout << false << "\n";
std::cout << std::boolalpha;
std::cout << false << "\n";
return 0;
}
And the result is (as you'd probably expect):
0
faux
Best way to print the result of a bool as 'false' or 'true' in c?
You could use C's conditional (or ternary) operator :
(a > b) ? "True" : "False";
or perhaps in your case:
x ? "True" : "False" ;
printing boolean result in C
putchar
prints a character. By printing the values 0 and 1, you're printing the null and start of heading (SOH) characters, both control characters. You'll need to convert the numbers 0 and 1 to something that's printable, either by calculating a printable value directly from the 0 or 1:
while (...) {
// note: the C standard (§ 5.2.1-3 of C99) ensures '0'+1 is '1', which is printable
putchar(c+'0');
}
or using c
to decide what to print.
while (...) {
if (c) {
...
} else {
...
}
// or:
//putchar(c ? ... : ...);
// though feelings on the ternary operator vary.
}
printing the value of bool data type in c++
Try this :
bool b1 = false;
bool b2 = true;
std::cout << std::boolalpha << b1 << std::endl;
std::cout << std::noboolalpha << b1 << std::endl;
std::cout << std::boolalpha << b2 << std::endl;
std::cout << std::noboolalpha << b2 << std::endl;
Output :
false
0
true
1
behavior of print function inside a Boolean expression
According to the C Standard (6.5.13 Logical AND operator)
4 Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; if the second operand is evaluated, there is
a sequence point between the evaluations of the first and second
operands. If the first operand compares equal to 0, the second
operand is not evaluated.
In the expression used as an initializer in this declaration
bool res = ((a == b) && printf("your "));
the first operand (a == b)
of the logical AND operator evaluates to 0. So the second operand that is the call of printf
is not evaluated,
On the other hand, in this expression used as an initializer in the declaration
bool res = (!(a == b) && printf("your "));
the first operand !(a == b)
evaluates to 1. So the second operand that is the call of printf
is also evaluated.
Related Topics
C++11 - Static_Assert Within Constexpr Function
Is There Any Reason to Use This-≫
How to Declare a Templated Struct/Class as a Friend
How to Return a Std::String.C_Str()
Difference Between Undefined Behavior and Ill-Formed, No Diagnostic Message Required
Why Does Printf() Promote a Float to a Double
How to Emulate Template≪Auto X≫
Determine If a Type Is an Stl Container At Compile Time
Append an Int to a Std::String
How Does This Template Magic Determine Array Parameter Size
Using Unicode in C++ Source Code
Conversion of 2D Array to Pointer-To-Pointer
How to Pass a Reference to a Two-Dimensional Array to a Function
What Techniques Can Be Used to Speed Up C++ Compilation Times
How to Call a Function on All Variadic Template Args