Cover Image for Arithmetic Operator in C
88 views

Arithmetic Operator in C

The arithmetic operators are used to perform mathematical operations on numeric operands, such as integers and floating-point numbers. Here are the common arithmetic operators in C:

  1. Addition (+): Adds two operands to produce their sum.
C
 int result = 5 + 3; // result will be 8
  1. Subtraction (-): Subtracts the right operand from the left operand to produce the difference.
C
 int result = 10 - 4; // result will be 6
  1. Multiplication (*): Multiplies two operands to produce their product.
C
 int result = 6 * 7; // result will be 42
  1. Division (/): Divides the left operand by the right operand to produce the quotient (integer division for integers).
C
 int result = 10 / 3; // result will be 3 (integer division)
  1. Modulus (%): Computes the remainder when the left operand is divided by the right operand.
C
 int result = 10 % 3; // result will be 1 (remainder)
  1. Increment (++): Increases the value of a variable by 1. It can be used as a prefix (++i) or a postfix (i++) operator.
C
 int i = 5;
 i++; // i is now 6
  1. Decrement (--): Decreases the value of a variable by 1. It can be used as a prefix (--i) or a postfix (i--) operator.
C
 int i = 5;
 i--; // i is now 4

These arithmetic operators can be used with various numeric data types, including int, float, double, and more. The behavior of these operators may vary depending on the data types of the operands. For example, division with integer operands results in integer division, while division with floating-point operands results in floating-point division.

Additionally, you can use parentheses to control the order of operations, just as you would in algebra. For example, (2 + 3) * 4 will yield a different result than 2 + (3 * 4).

Keep in mind issues related to integer division, such as truncation of decimal places, when working with integers and the division operator /. To perform floating-point division, you may need to use data types like float or double.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS