<p>Maybe this is a dumb question, but is there any way to convert a boolean value to a string such that 1 turns to "true" and 0 turns to "false"?  I could just use an if statement, but it would be nice to know if there is a way to do that with the language or standard libraries.  Plus, I'm a pedant.  :)</p>

[{'answer_id': 29394, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': -1, 'selected': False, 'text': '<p>I agree that a macro might be the best fit.  I just whipped up a test case (believe me I\'m no good with C/C++ but this sounded fun):</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdarg.h&gt;\n\n#define BOOL_STR(b) (b?"true":"false")\n\nint main (int argc, char const *argv[]) {\n    bool alpha = true;\n    printf( BOOL_STR(alpha) );\n    return 0;\n}\n</code></pre>\n'}, {'answer_id': 29396, 'author': 'dagorym', 'author_id': 171, 'author_profile': 'https://Stackoverflow.com/users/171', 'pm_score': -1, 'selected': False, 'text': '<p>Try this Macro.  Anywhere you want the "true" or false to show up just replace it with PRINTBOOL(var) where var is the bool you want the text for.</p>\n\n<pre><code>#define PRINTBOOL(x) x?"true":"false"\n</code></pre>\n'}, {'answer_id': 29509, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>I use a ternary in a printf like this:</p>\n\n<pre><code>printf("%s\\n", b?"true":"false");\n</code></pre>\n\n<p>If you macro it :</p>\n\n<pre><code>B2S(b) ((b)?"true":"false")\n</code></pre>\n\n<p>then you need to make sure whatever you pass in as <code>\'b\'</code> doesn\'t have any side effects.  And don\'t forget the brackets around the <code>\'b\'</code> as you could get compile errors.</p>\n'}, {'answer_id': 29536, 'author': 'dwj', 'author_id': 346, 'author_profile': 'https://Stackoverflow.com/users/346', 'pm_score': 2, 'selected': False, 'text': '<p>If you decide to use macros (or are using C on a future project) you should add parenthesis around the \'b\' in the macro expansion (I don\'t have enough points yet to edit other people\'s content):</p>\n\n<pre><code>#define BOOL_STR(b) ((b)?"true":"false")\n</code></pre>\n\n<p>This is a <a href="http://www.embedded.com/1999/9912/9912feat1.htm" rel="nofollow noreferrer">defensive programming</a> technique that protects against hidden order-of-operations errors; i.e., how does this evaluate for <em>all</em> compilers?</p>\n\n<pre><code>1 == 2 ? "true" : "false"\n</code></pre>\n\n<p>compared to </p>\n\n<pre><code>(1 == 2) ? "true" : "false"\n</code></pre>\n'}, {'answer_id': 29571, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 8, 'selected': True, 'text': '<p>How about using the C++ language itself?</p>\n\n<pre><code>bool t = true;\nbool f = false;\nstd::cout &lt;&lt; std::noboolalpha &lt;&lt; t &lt;&lt; " == " &lt;&lt; std::boolalpha &lt;&lt; t &lt;&lt; std::endl;        \nstd::cout &lt;&lt; std::noboolalpha &lt;&lt; f &lt;&lt; " == " &lt;&lt; std::boolalpha &lt;&lt; f &lt;&lt; std::endl;\n</code></pre>\n\n<p>UPDATE: </p>\n\n<p>If you want more than 4 lines of code without any console output, please go to <a href="https://en.cppreference.com/w/cpp/io/manip/boolalpha" rel="noreferrer">cppreference.com\'s page talking about <code>std::boolalpha</code> and <code>std::noboolalpha</code></a> which shows you the console output and explains more about the API. </p>\n\n<p>Additionally using <code>std::boolalpha</code> will modify the global state of <code>std::cout</code>, you may want to restore the original behavior <a href="https://stackoverflow.com/q/2273330/52074">go here for more info on restoring the state of <code>std::cout</code></a>.</p>\n'}, {'answer_id': 29798, 'author': 'OJ.', 'author_id': 611, 'author_profile': 'https://Stackoverflow.com/users/611', 'pm_score': 7, 'selected': False, 'text': '<p>We\'re talking about C++ right? Why on earth are we still using macros!?</p>\n\n<p>C++ inline functions give you the same speed as a macro, with the added benefit of type-safety and parameter evaluation (which avoids the issue that Rodney and dwj mentioned.</p>\n\n<pre><code>inline const char * const BoolToString(bool b)\n{\n  return b ? "true" : "false";\n}\n</code></pre>\n\n<p>Aside from that I have a few other gripes, particularly with the accepted answer :)</p>\n\n<pre><code>// this is used in C, not C++. if you want to use printf, instead include &lt;cstdio&gt;\n//#include &lt;stdio.h&gt;\n// instead you should use the iostream libs\n#include &lt;iostream&gt;\n\n// not only is this a C include, it\'s totally unnecessary!\n//#include &lt;stdarg.h&gt;\n\n// Macros - not type-safe, has side-effects. Use inline functions instead\n//#define BOOL_STR(b) (b?"true":"false")\ninline const char * const BoolToString(bool b)\n{\n  return b ? "true" : "false";\n}\n\nint main (int argc, char const *argv[]) {\n    bool alpha = true;\n\n    // printf? that\'s C, not C++\n    //printf( BOOL_STR(alpha) );\n    // use the iostream functionality\n    std::cout &lt;&lt; BoolToString(alpha);\n    return 0;\n}\n</code></pre>\n\n<p>Cheers :)</p>\n\n<hr>\n\n<p>@DrPizza: Include a whole boost lib for the sake of a function this simple? You\'ve got to be kidding?</p>\n'}, {'answer_id': 29831, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 4, 'selected': False, 'text': '<p>C++ has proper strings so you might as well use them.  They\'re in the standard header string.  #include &lt;string&gt; to use them. No more strcat/strcpy buffer overruns; no more missing null terminators; no more messy manual memory management; proper counted strings with proper value semantics.</p>\n\n<p>C++ has the ability to convert bools into human-readable representations too.  We saw hints at it earlier with the iostream examples, but they\'re a bit limited because they can only blast the text to the console (or with fstreams, a file).  Fortunately, the designers of C++ weren\'t complete idiots; we also have iostreams that are backed not by the console or a file, but by an automatically managed string buffer.  They\'re called stringstreams.  #include &lt;sstream&gt; to get them. Then we can say:</p>\n\n<pre><code>std::string bool_as_text(bool b)\n{\n    std::stringstream converter;\n    converter &lt;&lt; std::boolalpha &lt;&lt; b;   // flag boolalpha calls converter.setf(std::ios_base::boolalpha)\n    return converter.str();\n}\n</code></pre>\n\n<p>Of course, we don\'t really want to type all that.  Fortunately, C++ also has a convenient third-party library named <a href="http://www.boost.org/" rel="nofollow noreferrer">Boost</a> that can help us out here.  Boost has a nice function called lexical_cast.  We can use it thus:</p>\n\n<pre><code>boost::lexical_cast&lt;std::string&gt;(my_bool)\n</code></pre>\n\n<p>Now, it\'s true to say that this is higher overhead than some macro; stringstreams deal with locales which you might not care about, and create a dynamic string (with memory allocation) whereas the macro can yield a literal string, which avoids that.  But on the flip side, the stringstream method can be used for a great many conversions between printable and internal representations. You can run \'em backwards; boost::lexical_cast&lt;bool&gt;("true") does the right thing, for example.  You can use them with numbers and in fact any type with the right formatted I/O operators.  So they\'re quite versatile and useful.</p>\n\n<p>And if after all this your profiling and benchmarking reveals that the lexical_casts are an unacceptable bottleneck, <em>that\'s</em> when you should consider doing some macro horror.</p>\n'}, {'answer_id': 29907, 'author': 'Shadow2531', 'author_id': 1697, 'author_profile': 'https://Stackoverflow.com/users/1697', 'pm_score': 4, 'selected': False, 'text': '<p>This should be fine:</p>\n\n<pre><code>\nconst char* bool_cast(const bool b) {\n    return b ? "true" : "false";\n}\n</code></pre>\n\n<p>But, if you want to do it more C++-ish:</p>\n\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;sstream&gt;\nusing namespace std;\n\nstring bool_cast(const bool b) {\n    ostringstream ss;\n    ss &lt;&lt; boolalpha &lt;&lt; b;\n    return ss.str();\n}\n\nint main() {\n    cout &lt;&lt; bool_cast(true) &lt;&lt; "\\n";\n    cout &lt;&lt; bool_cast(false) &lt;&lt; "\\n";\n}\n</code></pre>\n'}, {'answer_id': 30122, 'author': 'Mat Noguchi', 'author_id': 1799, 'author_profile': 'https://Stackoverflow.com/users/1799', 'pm_score': -1, 'selected': False, 'text': "<p>As long as strings can be viewed directly as a char array it's going to be really hard to convince me that <code>std::string</code> represents strings as first class citizens in C++.</p>\n\n<p>Besides, combining allocation and boundedness seems to be a bad idea to me anyways.</p>\n"}, {'answer_id': 49032470, 'author': 'Erwan Guiomar', 'author_id': 7797382, 'author_profile': 'https://Stackoverflow.com/users/7797382', 'pm_score': 0, 'selected': False, 'text': '<p>This post is old but now you can use <code>std::to_string</code> to convert a lot of variable as <code>std::string</code>.</p>\n\n<p><a href="http://en.cppreference.com/w/cpp/string/basic_string/to_string" rel="nofollow noreferrer">http://en.cppreference.com/w/cpp/string/basic_string/to_string</a></p>\n'}, {'answer_id': 50468243, 'author': 'UIResponder', 'author_id': 2776045, 'author_profile': 'https://Stackoverflow.com/users/2776045', 'pm_score': 0, 'selected': False, 'text': '<p>Use <code>boolalpha</code> to print bool to string. </p>\n\n<pre><code>std::cout &lt;&lt; std::boolalpha &lt;&lt; b &lt;&lt; endl;\nstd::cout &lt;&lt; std::noboolalpha &lt;&lt; b &lt;&lt; endl;\n</code></pre>\n\n<p><a href="http://www.cplusplus.com/reference/ios/boolalpha/" rel="nofollow noreferrer">C++ Reference</a></p>\n'}, {'answer_id': 53763374, 'author': 'Federico Spinelli', 'author_id': 6800578, 'author_profile': 'https://Stackoverflow.com/users/6800578', 'pm_score': 2, 'selected': False, 'text': '<p>With C++11 you might use a lambda to get a slightly more compact code and in place usage:</p>\n\n<pre><code>bool to_convert{true};\nauto bool_to_string = [](bool b) -&gt; std::string {\n    return b ? "true" : "false";\n};\nstd::string str{"string to print -&gt; "};\nstd::cout&lt;&lt;str+bool_to_string(to_convert);\n</code></pre>\n\n<p>Prints:</p>\n\n<pre><code>string to print -&gt; true\n</code></pre>\n'}, {'answer_id': 57337518, 'author': 'ewd', 'author_id': 1803860, 'author_profile': 'https://Stackoverflow.com/users/1803860', 'pm_score': 2, 'selected': False, 'text': '<p>Without dragging ostream into it:</p>\n\n<pre><code>constexpr char const* to_c_str(bool b) {\n   return  \n    std::array&lt;char const*, 2&gt;{"false", "true "}[b]\n   ;\n};\n</code></pre>\n'}, {'answer_id': 63468199, 'author': 'carlsb3rg', 'author_id': 384024, 'author_profile': 'https://Stackoverflow.com/users/384024', 'pm_score': 0, 'selected': False, 'text': '<p>How about the simple:</p>\n<pre><code>constexpr char const* toString(bool b)\n{\n   return b ? &quot;true&quot; : &quot;false&quot;;\n}\n</code></pre>\n'}, {'answer_id': 64771898, 'author': 'Ciro Santilli OurBigBook.com', 'author_id': 895245, 'author_profile': 'https://Stackoverflow.com/users/895245', 'pm_score': 3, 'selected': False, 'text': '<p><strong>C++20 <code>std::format(&quot;{}&quot;</code></strong></p>\n<p><a href="https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification" rel="nofollow noreferrer">https://en.cppreference.com/w/cpp/utility/format/formatter#Standard_format_specification</a> claims that the default output format will be the string by default:</p>\n<blockquote>\n<pre><code>#include &lt;format&gt;\nauto s6 = std::format(&quot;{:6}&quot;, true);  // value of s6 is &quot;true  &quot;\n</code></pre>\n</blockquote>\n<p>and:</p>\n<blockquote>\n<p>The available bool presentation types are:</p>\n<ul>\n<li>none, s: Copies textual representation (true or false, or the locale-specific form) to the output.</li>\n<li>b, B, c, d, o, x, X: Uses integer presentation types with the value static_cast(value).</li>\n</ul>\n</blockquote>\n<p>The existing <code>fmt</code> library implements it for before it gets official support: <a href="https://github.com/fmtlib/fmt" rel="nofollow noreferrer">https://github.com/fmtlib/fmt</a> Install on Ubuntu 22.04:</p>\n<pre><code>sudo apt install libfmt-dev\n</code></pre>\n<p>Modify source to replace:</p>\n<ul>\n<li><code>&lt;format&gt;</code> with <code>&lt;fmt/core.h&gt;</code></li>\n<li><code>std::format</code> to <code>fmt::format</code></li>\n</ul>\n<p>main.cpp</p>\n<pre><code>#include &lt;string&gt;\n#include &lt;iostream&gt;\n\n#include &lt;fmt/core.h&gt;\n\nint main() {\n    std::string message = fmt::format(&quot;The {} answer is {}.&quot;, true, false);\n    std::cout &lt;&lt; message &lt;&lt; std::endl;\n}\n</code></pre>\n<p>and compile and run with:</p>\n<pre><code>g++ -std=c++11 -o main.out main.cpp -lfmt\n./main.out\n</code></pre>\n<p>Output:</p>\n<pre><code>The true answer is false.\n</code></pre>\n<p>Related: <a href="https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf/57286312#57286312">std::string formatting like sprintf</a></p>\n'}, {'answer_id': 67378885, 'author': 'space.cadet', 'author_id': 3430981, 'author_profile': 'https://Stackoverflow.com/users/3430981', 'pm_score': 1, 'selected': False, 'text': "<p>A really quick and clean solution, if you're only doing this once or don't want to change the global settings with bool alpha, is to use a ternary operator directly in the stream, like so:</p>\n<pre><code>bool myBool = true;\nstd::cout &lt;&lt; &quot;The state of myBool is: &quot; &lt;&lt; (myBool ? &quot;true&quot; : &quot;false&quot;) &lt;&lt; std::endl;\nenter code here\n</code></pre>\n<p>Ternarys are easy to learn. They're just an IF statement on a diet, that can be dropped pretty well anywhere, and:</p>\n<pre><code>(myBool ? &quot;true&quot; : &quot;false&quot;)\n</code></pre>\n<p>is pretty well this (sort of):</p>\n<pre><code>{\n   if(myBool){\n      return &quot;true&quot;;\n   } else {\n      return &quot;false&quot;;\n   }\n}\n</code></pre>\n<p>You can find all kinds of fun uses for ternarys, including here, but if you're always using them to output a &quot;true&quot; &quot;false&quot; into the stream like this, you should just turn the boolalpha feature on, unless you have some reason not to:</p>\n<pre><code>std::cout &lt;&lt; std::boolalpha;\n</code></pre>\n<p>somewhere at the top of your code to just turn the feature on globally, so you can just drop those sweet sweet booleans right into the stream and not worry about it.</p>\n<p>But don't use it as a tag for one-off use, like this:</p>\n<pre><code>std::cout &lt;&lt; &quot;The state of myBool is: &quot; &lt;&lt; std::boolalpha &lt;&lt; myBool &lt;&lt; std::noboolalpha;\n</code></pre>\n<p>That's a lot of unnecessary function calls and wasted performance overhead and for a single bool, when a simple ternary operator will do.</p>\n"}, {'answer_id': 73484243, 'author': 'David - Walter', 'author_id': 19843777, 'author_profile': 'https://Stackoverflow.com/users/19843777', 'pm_score': -1, 'selected': False, 'text': '<pre><code>#include &lt;iostream&gt;\n#include &lt;string&gt;\n\nusing namespace std;\n\nstring toBool(bool boolean)\n{\n    string result;\n    if(boolean == true)\n        result = &quot;true&quot;;\n    else \n        result = &quot;false&quot;;\n    return result;\n}   \n\nint main()\n{\n     bool myBoolean = true;          //Boolean\n     string booleanValue;\n     booleanValue = toBool(myBoolean);\n     cout &lt;&lt; &quot;bool: &quot; &lt;&lt; booleanValue &lt;&lt; &quot;\\n&quot;;\n }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/29383', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2147/']