C++ Reference ยท Operators
Alternative operator tokens: and, or, not
Reference for C++ alternative operator tokens such as and, or, not, bitand, xor, plus digraphs and trigraph history, with the full mapping table.
C++ defines word spellings for several operators. and means &&, or means ||, not means !, and so on. They are not macros, as the similar names in C's <iso646.h> are: they are real tokens built into the language, so they are always available and cannot be redefined. They exist for keyboards and character sets that lack characters such as &, |, ^, ~ or !.
Most code uses the symbols, but some teams prefer and/or/not for readability in conditions. Both spellings are 100% equivalent and can be mixed, although mixing in one expression looks odd.
if (not done and (a or b)) { /* same as !done && (a || b) */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
| Alternative | Symbol | Meaning |
|---|---|---|
and |
&& |
logical AND |
or |
|| |
logical OR |
not |
! |
logical NOT |
not_eq |
!= |
inequality |
bitand |
& |
bitwise AND (and address-of) |
bitor |
| |
bitwise OR |
xor |
^ |
bitwise XOR |
compl |
~ |
bitwise complement |
and_eq |
&= |
AND assignment |
or_eq |
|= |
OR assignment |
xor_eq |
^= |
XOR assignment |
Digraphs: <: is [, :> is ], <% is {, %> is }, %: is #, and %:%: is ##. (Trigraphs such as ??= were removed in C++17.)
Errors: none specific. Using one of these words as a variable, function or macro name is ill-formed.
Usage notes
- Same precedence: an alternative token has exactly the precedence and associativity of its symbol;
a and b or cis(a && b) || c. See precedence. - Bitwise versus logical:
xoris the bitwise^and has no logical counterpart; onbooloperands it gives the logical exclusive-or result, but there is no short-circuiting.bitandandandare different operators. - Overloading: write
operator&&oroperator and; both name the same function. Using the symbol form in declarations is the usual convention. - Unary
bitandis the address-of operator:bitand xis&x. This is legal but confusing; don't write it. - Reserved names: because they are tokens,
and,or,not,xor,compl,bitand,bitor,and_eq,or_eq,xor_eqandnot_eqcan't name your own variables. This breaks code ported from C that used them as identifiers, and C programmers need<iso646.h>to get the same spellings. - Microsoft's compiler needs
/permissive-(default in newer language modes) or<iso646.h>for these words; GCC and Clang always accept them. - Style: most style guides use the symbols; choose one spelling per code base. The words read well for
notandandin conditions but makebitand/xorhard to scan. - Digraphs exist only for restricted character sets; one place you may meet them is
<::handling in templates (std::vector<::std::string>is fine since C++11, because<:is not treated as a digraph before::).