Flow Control & Operators
Operators
In C and C++, operators are special symbols that represent computations or operations that are performed on variables and values. They are categorised based on the number of operands they work on (unary, binary, or ternary) and their functionality. Here’s a comprehensive list:
Arithmetic
- Unary:
+
: Unary plus-
: Unary minus++
: Increment--
: Decrement
- Binary:
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus
int a = 1;
// prefix
int b = ++a; // b = 2 , a = 2
// postfix ()
b = a++; // b = 2, a = 3
Comparison (Relational)
all of these operators return true or false.
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Logical
&&
: Logical AND||
: Logical OR!
: Logical NOT
Bitwise
&
: Bitwise AND|
: Bitwise OR^
: Bitwise XOR (Exclusive OR)~
: Bitwise NOT<<
: Left shift (multiply by 2)>>
: Right shift (divid by 2)
Assignment
=
: Simple assignment+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign%=
: Modulus and assign&=
: Bitwise AND and assign|=
: Bitwise OR and assign^=
: Bitwise XOR and assign<<=
: Left shift and assign>>=
: Right shift and assign
Preprocessor
#
: Stringizing operator (converts macro parameter to a string)##
: Token-pasting operator (concatenates two tokens)