Arithmetic Operators - cradules/cs50 GitHub Wiki

Video Operators

Arithmetic Operators

  • In order to manipulate and work with variables and values in C, we are have a number of operators at our disposal.

  • Let's take a look at some of these now.

  • In C we can add(+), subtract(-), multiply(*) and divide(/) numbers, as expected:

    • int x = y +1;
    • x = x * 5;
  • We also have the modulus operator(%). witch give us the reminder when the number on the left of the operator is divided by the number on the right.

    • int m = 13 % 4; //m now is 1
  • C also provides a shorthand way to apply an arithmetic operator to a single variable

    • x = x * 5;
    • x *=5
  • This trick works with all five basic arithmetic operators. C provides a further shorthand for incrementing of decrementing a variable by 1:

    • x++;
    • x--;