0

C Arithmetic Operators

Similar to other programming languages, C has five arithmetic operators:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modular)

If you don’t know the modular (%) operator, it’s the reminder of a division (/). For example, 15 % 6 is equal to 3.

These operators have the following precedence:

  1. Multiplication, division, and modular get processed first
  2. Addition and subtraction get processed last

Use parenthesis and square brackets to override the precedence.

Here is the example source code I wrote:

#include <stdio.h>

main() {
 int x, y, z;

  // Calculation through declaring variables
 x = 9;
 y = (x + 6) / 3;
 z = x * y + 1 + 2 - 3 * 4;
 x = (10 + 35) % 12;
 printf("x = %d, y = %d, z = %dn", x, y, z);

  // Calculate directly without declaring variable
 printf("Answer is %dn", 3 * 17);

 fflush(stdin);
 getchar();
}

Below is the output:

x = 9, y = 5, z = 36
Answer is 51