Operators

Everyone knows math, right?

Operators

Operators help us to perform the most fundamental mathematical or logical operations on variables.

Operators can accept different numbers of operands. Their names are as follows:

  • Unary Operators: (operators that work with only 1 "variable")

    Takes a single operand +x -x *x ++x --x x++ x-- !x ~x

  • Binary Operators: (operators that work with 2 "variables")

    Takes two operands x + y x - y x * y x / y x % y x && y x || y

  • Ternary Operator: (operators that work with 3 "variables")

    Takes three operands x ? y : z

Beware that different operators carry different precedences, meaning values are evaluated from the operator with the highest precedence down to the lowest, like how multiplication comes first before addition in maths.

Assignment Operators

  • = assignment (B)

  • ...

Compound Assignment Operators

These operators merge arithmetic and assignment into one operation. For example:

  • +=: Adds right operand to left operand, and assigns the resultant value to left operand.

int a = 1;
int b = 2;

a += b; // equal to a = a + b

There are more assignment operators (e.g. -=, /=, %=). Read this for more details.

Arithmetic Operators

These are your typical mathematical operators:

  • + addition (B)

  • + unary plus (UB. no effect, just for symmetry)

  • - subtraction (B)

  • - unary minus (UB)

  • * multiplication (B)

  • / division (B)

  • % modulo (B)

  • ...

#include <stdio.h>

int main() {
    printf("addition: %d\n", 2 + 3); // prints 5
    printf("subtraction: %d\n", 20 - 8); // prints 12
    printf("multiplication: %d\n", 500 * 4); // prints 2000
    printf("division (int): %d\n", 5 / 2); // prints 2
    printf("division (float): %f\n", 5.0f / 2.0f); // prints 2.5
    printf("modulo: %d\n", 13 % 5); // prints 3
    
    return 0;
}

⚠️ Warning: Note the two different divisions in the example above. In math, 5 / 2 = 2.5. However, in C, both 5 and 2 are integers, so integer division is performed. This means the decimal part is discarded giving 2 as its result. In contrast to this, 5.0f and 2.0f are floats, so floating-point division is performed and the decimal part is retained, giving (about) 2.5 as its result.

Relational Operators

A relational operator compares the value of two expressions, most of which are self-explanatory. These operators take two operators and return true/1 or false/0.

  • == equal to (B)

  • != not equal to (B)

  • > greater than (B)

  • >= greater than or equal to (B)

  • < less than (B)

  • <= less than or equal to (B)

:Warning: Warning: Do not mix up = and ==. One is used for value assignment, the other used to compare whether the values are the same.

Logical Operators

  • ||: Logical "or". Returns true if left or right operands are non-zero.

  • &&: Logical "and". Returns true if left and right operands are non-zero

  • ! Logical "not". Reverses the truthness of its operand.

These operators accept boolean-represented values on both siesidess and perform logical operations on them to return a boolean value.

⚠️ Warning: ! has a higher precedence than || and &&.

Truth Table

PQP or QP and Qnot P

false

false

false

false

true

false

true

true

false

true

true

false

true

false

false

true

true

true

true

false

Read Operators in C and C++ on Wikipedia for more information, including Bitwise operators, compound assignment operators, Member and pointer operators, etc.

💡TIPS: Conditions can be chained by logical operators. For example:

int_a != int_b || !(int_c > int_d)

Ternary Operator

The ternary operator is also known as the conditional operator. It is similar to if...else..., but is faster to type and is considered a shortcut for if. We will cover more about this after the [If] if statement.

Last updated