Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/bool literal"

From cppreference.com
< cpp‎ | language
m (langlinks)
m (Keywords)
 
(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{title|Boolean literals}}
 
{{title|Boolean literals}}
 
{{cpp/language/expressions/navbar}}
 
{{cpp/language/expressions/navbar}}
 +
 
===Syntax===
 
===Syntax===
 
{{sdsc begin}}
 
{{sdsc begin}}
{{sdsc | num = 1 | {{ttb|true}} }}
+
{{sdsc|num=1|{{ttb|true}}}}
{{sdsc | num = 2 | {{ttb|false}} }}
+
{{sdsc|num=2|{{ttb|false}}}}
 
{{sdsc end}}
 
{{sdsc end}}
 +
 
===Explanation===
 
===Explanation===
The Boolean literals are the keywords {{tt|true}} and {{tt|false}}. They are {{rlp|value_category|prvalues}} of type {{rlp|types#Boolean_type|bool}}.
+
The Boolean literals are the keywords {{c|true}} and {{c|false}}. They are {{rlp|value category#prvalue|prvalues}} of type {{rlpt|types#Boolean type|bool}}.
  
 
===Notes===
 
===Notes===
 
See {{rlp|implicit_cast#Integral_conversions|Integral conversions}} for implicit conversions from {{tt|bool}} to other types and {{rlp|implicit_cast#Boolean conversions|boolean conversions}} for the implicit conversions from other types to {{tt|bool}}.
 
See {{rlp|implicit_cast#Integral_conversions|Integral conversions}} for implicit conversions from {{tt|bool}} to other types and {{rlp|implicit_cast#Boolean conversions|boolean conversions}} for the implicit conversions from other types to {{tt|bool}}.
 +
 +
===Keywords===
 +
{{ltt|cpp/keyword/false}},
 +
{{ltt|cpp/keyword/true}}
  
 
===Example===
 
===Example===
Line 18: Line 24:
 
int main()
 
int main()
 
{
 
{
  std::cout << std::boolalpha
+
    std::cout << std::boolalpha
            << true << '\n'
+
              << true << '\n'
            << false << '\n'
+
              << false << '\n'
            << std::noboolalpha
+
              << std::noboolalpha
            << true << '\n'
+
              << true << '\n'
            << false << '\n';
+
              << false << '\n';
 
}
 
}
 
|output=
 
|output=
Line 31: Line 37:
 
0
 
0
 
}}
 
}}
 +
 +
===References===
 +
{{ref std c++23}}
 +
{{ref std|section= 5.13.6|title=Boolean literals|id=lex.bool}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section= 5.13.6|title=Boolean literals|id=lex.bool}}
 +
{{ref std end}}
 +
{{ref std c++17}}
 +
{{ref std|section= 5.13.6|title=Boolean literals|id=lex.bool}}
 +
{{ref std end}}
 +
{{ref std c++14}}
 +
{{ref std|section= 2.13.6|title=Boolean literals|id=lex.bool}}
 +
{{ref std end}}
 +
{{ref std c++11}}
 +
{{ref std|section= 2.13.6|title=Boolean literals|id=lex.bool}}
 +
{{ref std end}}<!-- N1804 was used for this
 +
{{ref std c++03}}
 +
{{ref std|section= 2.13.5|title=Boolean literals|id=lex.bool}}
 +
{{ref std end}}-->
 +
{{ref std c++98}}
 +
{{ref std|section= 2.13.5|title=Boolean literals|id=lex.bool}}
 +
{{ref std end}}
 +
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc see c|c/language/bool_constant|Predefined Boolean constants|nomono=true}}
 +
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 14:58, 12 August 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

Contents

[edit] Syntax

true (1)
false (2)

[edit] Explanation

The Boolean literals are the keywords true and false. They are prvalues of type bool.

[edit] Notes

See Integral conversions for implicit conversions from bool to other types and boolean conversions for the implicit conversions from other types to bool.

[edit] Keywords

false, true

[edit] Example

#include <iostream>
 
int main()
{
    std::cout << std::boolalpha
              << true << '\n'
              << false << '\n'
              << std::noboolalpha
              << true << '\n'
              << false << '\n';
}

Output:

true
false
1
0

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 5.13.6 Boolean literals [lex.bool]
  • C++20 standard (ISO/IEC 14882:2020):
  • 5.13.6 Boolean literals [lex.bool]
  • C++17 standard (ISO/IEC 14882:2017):
  • 5.13.6 Boolean literals [lex.bool]
  • C++14 standard (ISO/IEC 14882:2014):
  • 2.13.6 Boolean literals [lex.bool]
  • C++11 standard (ISO/IEC 14882:2011):
  • 2.13.6 Boolean literals [lex.bool]
  • C++98 standard (ISO/IEC 14882:1998):
  • 2.13.5 Boolean literals [lex.bool]

[edit] See also

C documentation for Predefined Boolean constants