-
Notifications
You must be signed in to change notification settings - Fork 134
Operators
Kirk Pearce edited this page Dec 8, 2016
·
4 revisions
There are several types of operators available in C/C++:
Note: this list is not exhaustive and some symbols have multiple operator definitions depending on the context, e.g. the *
operator can be used for multiplication or to dereference a pointer. (Pointers/references will be covered later in the tutorial).
Operator | Action | Example |
---|---|---|
= | Assignment | x = y |
+ | Addition | c = a+b |
- | Subtraction | c = a-b |
* | Multiplication | c = a*b |
/ | Division | c = a/b |
% | Modulo (remainder after division) | rem = a%b |
+= | Add then assign |
a += b (equivalent to a = a+b ) |
-= | Subtract then assign |
a-=b (equivalent to a = a-b ) |
*= | Multiply then assign |
a*=b (equivalent to a = a*b ) |
/= | Divide then assign |
a /=b (equivalent to a = a/b ) |
++ | Increment |
a = 0; a++; (a now is 1 ) |
-- | Decrement |
a = 1; a--; (a is now 0) |
Operator | Action | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Note: Boolean operator statements return zero if false and non-zero if true
Operator | Action | Example |
---|---|---|
&& | And | (a == b) && (b == c) |
|| | Or | ` (a == b) |
! | Not | !(a == b) |