Arithmetic operators: +, -, *, /, %

Home / C Programming / Arithmetic operators: +, -, *, /, %

Arithmetic operators: +, -, *, /, %


Arithmetic operators are used in C programming to perform mathematical calculations on operands. Here are the arithmetic operators in C:

1.Addition (+): 

Adds two operands together.

   Example:

   int result = 5 + 3;  // result is 8

2. Subtraction (-):

Subtracts the second operand from the first operand.

   Example:

   int result = 5 - 3;  // result is 2


3. Multiplication (*):

Multiplies two operands together.

   Example:

    int result = 5 * 3;  // result is 15

4. Division (/):

Divides the first operand by the second operand.

   Example:

   float result = 10 / 3;  // result is 3.3333 (integer division truncates decimal part)

5. Modulus( %):

Returns the remainder after dividing the first operand by the second operand.

   Example:  

   int result = 10 % 3;  // result is 1 (remainder of 10 divided by 3)

Arithmetic operators follow the usual precedence rules, and parentheses can be used to specify the order of operations. For example, multiplication and division have higher precedence than addition and subtraction. If the operands of an arithmetic operation have different data types, C will perform type conversion based on certain rules (e.g., integer promotion).

It's important to note that division by zero (`0`) is undefined and may result in a runtime error. Also, the modulus operator (`%`) can only be used with integer operands.

You can combine arithmetic operators with assignment operators (`+=`, `-=`, `*=`, `/=`, `%=`) to perform arithmetic calculations and assign the result to a variable in a single step.

Example:

int num = 5;

num += 3;  // equivalent to num = num + 3; num is now 8

These arithmetic operators provide the basic building blocks for performing mathematical operations in C programming.