An operator is a symbol that tells the compiler to do something with one or more values. The values an operator works on are its operands. In 7 + 2, the + is the operator, 7 and 2 are its operands, and the result of the expression is 9.
C and C++ share most of their operators, so the same ideas apply to both. The examples here use C++ and std::cout. Most of the operators work the same way in C.
The main groups of operators
- Arithmetic:
+,-,*,/and%do math on numbers. For the details, see what are arithmetic operators. - Comparison:
==,!=,<,>,<=and>=compare two values and givetrueorfalse. - Logical:
&&(AND),||(OR) and!(NOT) combine true and false values. The OR operator in C++ goes deeper into||. - Bitwise:
&,|,^,~,<<and>>work on the individual bits of integers. - Assignment:
=stores a value in a variable.+=,-=and the others update a variable in one step. - Increment and decrement:
++and--add one or subtract one. The increment operator in C++ covers them in detail. - Conditional:
?:chooses between two values. - Pointer:
*(dereference) reads the value a pointer points to, and&(address-of) gives the address of a variable. - Member access and scope:
.and->reach the members of an object.::(C++ only) names something inside a namespace or class. The dot operator in c covers the first one. - sizeof:
sizeofgives the size of a type or object, in bytes.
An example program
This program uses several operators at once:
#include <iostream>
int main() {
int a = 10;
int b = 3;
std::cout << "a + b = " << a + b << "\n"; // 13
std::cout << "a > b = " << (a > b) << "\n"; // 1, meaning true
std::cout << "a % b = " << a % b << "\n"; // 1
bool bothPositive = a > 0 && b > 0;
std::cout << std::boolalpha << bothPositive << "\n"; // true
return 0;
}
Output:
a + b = 13
a > b = 1
a % b = 1
true
a + b adds the two numbers. a > b compares them, and the result prints as 1 because std::boolalpha is not set yet. a % b gives the remainder when 10 is divided by 3. The last line combines two comparisons with &&.
Unary, binary and ternary operators
Operators are also grouped by how many operands they take:
- Operators with one operand, such as
-xor!flag, are called unary operators in c. - Binary operators take two operands, as in
a + b. Most operators are binary. - The conditional operator in c,
?:, is the only ternary operator. It takes three operands.
Operator precedence
When an expression has more than one operator, precedence decides which one runs first. Multiplication, division and remainder run before addition and subtraction:
int result = 2 + 3 * 4; // 14, not 20
Use parentheses to say what you mean. (2 + 3) * 4 is 20.
Common beginner mistakes
- Using
=where you meant==.if (x = 5)assigns 5 toxand then tests 5, which is true.if (x == 5)compares instead. - Using
&where you meant&&.&works on bits, while&&checks true or false.&&also stops early when its left side isfalse. - Expecting a decimal from integer division.
7 / 2is3, because both operands are integers. Use7.0 / 2to get3.5.
Summary
An operator is a symbol that acts on its operands. The main groups are arithmetic, comparison, logical, bitwise, assignment, increment, conditional, pointer, member access and sizeof. Learn them one group at a time, and use parentheses whenever precedence might be unclear. Later, you can learn how operator overloading in c lets your own classes use these same symbols.