Table of Contents:
Overview
What are operators?
Operators are used to manipulate data. There are many different kinds of operators, such as assignment, arithmetic, logical, relational, and bitwise operators.
In C, the operators themselves are represented by symbols. For example, the addition operator would be the plus sign: +
. But what do these symbols represent in a program? Operators themselves are functions that accept arguments.
Binary, Unary, Prefix, Infix, and Postfix
An operand is what an operator operates on.
In mathematics an operand is the object of a mathematical operation
Operators will use one of the three 'fix' styles above, depending on the operator used.
Binary operators work with two operands. The addition +
function grabs the left operand and the right operand, adding them together, then returning the result.
Unary operators work with one operand. The increment operator is a good example of this:
int number = 10;
printf("This is the Postfix style: %d\n", number++);
// This is the Postfix style result: 10
printf("Now the variable number is assigned: %d\n", number);
// Now the variable number is assigned: 11
printf("This is the Prefix style result: %d\n", ++number);
// This is the Prefix style result: 12
printf("The variable number was already assigned: %d\n", number);
// The variable number was already assigned: 12
Assignment
Assignment operators use the Infix style. The values of the right operand are assigned to the left operand.
Numbers 1 - 6 are straight forward assignment operators. 7 & 8 are shift AND assignment operators. 9 - 11 are Bitwise assignment operators.
-
=
- Assignment -
+=
- Add AND assignment -
-=
- Subtract AND assignment -
*=
- Multiply AND assignment -
/=
- Division AND assignment -
%=
- Modulus AND assignment -
<<=
- Left shift AND assignment -
>>=
- Right shift AND assignment -
&=
- Bitwise AND assignment -
^=
- Bitwise exclusive OR and assignment -
|=
- Bitwise inclusive OR and assignment
You would typically use these operators to store calculated values into variables without writing out the calculation yourself.
Arithmetic
Most of the arithmetic operators use the Infix style to perform calculations on two operands. Numbers 6 and 7 can use either the Prefix or Postfix style.
-
+
- Addition -
-
- Subtraction -
*
- Multiplication -
\
- Division -
%
- Modulus -
++
- Increment -
--
- Decrement
int num = 1;
--num;
num++;
What is the difference in the increment/decrements 'fix' style? Running the operator before the variable num
ensures the value incremented/decremented is assigned and returned right away.
Running the increment/decrement operator after the variable num
will return the original value of the variable, and then assign the new value.
Logical
Logical operators use the Infix style, except number 3, which uses the Prefix style. These are boolean operators that return a true
or false
value. They evaluate the two operands and run them against each other to test equality in some way.
-
&&
- Logical AND (Returns true if both operands are true ) -
||
- Logical OR (Returns true if only one operand is true) -
!
- Logical NOT (Returns the inverse logical state of the operand it is being evoked on)
Relational
Relational operators also use the Infix style.
-
==
- if both operands are equal, return true. -
!=
- if the left operand is not equal to the right operand, return true -
>
- if the left operand is greater than the right operand, return true -
<
- if the left operand is less than the right operand, return true -
>=
- if the left operand is greater than or equal to the right operand, return true -
<=
- if the left operand is less than or equal to the right operand, return true
Bitwise
Bitwise operators are a bit different. These operators operate on the bits in integer values. They're also different from Logical operators, as Logical operators will use two of these symbols whereas Bitwise will only use one.
They all use the Infix style, except for number 4. Number 4 uses the Postfix style.
-
&
- Binary AND -
|
- Binary OR -
^
- Binary XOR -
~
- Binary One's Complement -
<<
- Binary Left Shift -
>>
- Binary Right Shift
I will go more in-depth on bitwise operators, operator precedence, plus other operators like cast and sizeof
in the next few entries of this series. Thank you for reading!
Top comments (0)