Arithmetic Operators in C Programming Language

Arithmetic Operators In C

They are the basic programming language tools used to perform some operations. In fact, these operators are used to manipulate variables and constants. The basic purpose of these operators is to combine the variables and constants via some basic manipulation (programming rules). The data that is used to manipulate using these operators are called operands. In C programming language an operator may be required one or more operands to act upon. C language supports many useful operators few of them are listed below.

  • Arithmetic operators.
  • Assignment operators.
  • Incrementoperators.
  • Decrement operators.
  • Relational operators.
  • Logical operators.
  • Conditional operator.
  • Comma operator.
  • sizeof operator.
  • Bitwise operators, etc.

Arithmetic Operators

These operators are used to perform some basic mathematical/arithmetic/numeric manipulations. These operators are also categorized into two important subcategories known as the Unary and the Binary Arithmetic Operators.

Unary Arithmetic Operators

These operators are used to manipulate only one single operand (variables/constants). For example: -a, here the (minus) ‘-‘ unary operator changes the sign of the variable ‘a’.

Binary Arithmetic Operators

These operators require exactly two operands to be manipulated or perform any arithmetic operation. The most commonly used binary operators are addition (+), subtraction (-), multiplication (*), and division (/).

Note: There is a significant difference between the unary and the binary Plus (+) and minus (-) operators. As the unary plus (+) or minus (-) operators required only one operand to be performed a calculation. While the binary addition (+) and subtraction (-) operators require exactly two operands to be performed any basic manipulation.

Integer Arithmetic Operation

To perform correct manipulation these operators require integer type operands and the final result that will be generated after manipulation will also be an integer. That means both operands and the resultant will always be an integer.

int x=9, y=5;
printf("Summation = %d\n", x+y);
printf("Difference = %d\n", x-y);
printf("Multiplication = %d\n", x*y);
printf("Division = %d\n", x/y);
Summation = 14
Difference = 4
Multiplication = 45
Division = 1

Note: While on dividing 9 by 5 the result must be 1.8 but the division operator truncates the decimal part and gives the remaining part.

Floating-Point Arithmetic

If the operands and the result after manipulation of these operands are always of floating-point type then such arithmetic is called floating-point arithmetic. To perform such arithmetic correctly the input must be of the floating-point type and the final result it will produce will also be the floating-point type.

int x=9.2, y=5.4;
printf("Summation = %.2f\n", x+y);
printf("Difference = %.2f\n", x-y);
printf("Multiplication = %.2f\n", x*y);
printf("Division = %.2f\n", x/y);
Summation = 14.60
Difference = 3.80
Multiplication = 49.68
Division = 1.70

Mixed Arithmetic

One integer and a floating-point number are manipulated to give a floating-point result. Such manipulation or operation is called the mixed arithmetic operation in C programming.

Modulus Arithmetic Operator (%)

This operator produces the remainder after manipulation and it always operates with integer inputs/variables/operands.

int x=9, y=5;
printf("Remainder = %d\n", x%y);
Remaider = 1

Assignment Operator

This is nothing but the equal to sign of mathematics. This operator is basically used to store a value to the variable. This is why this operator is also named as the assignment operator. There will be three terms in a statement where the assignment operator is used. The first one will be the name of the variable, next to the assignment operator and finally, the third one will be the value of the variable.

Example: x = 4, The integer value 4 is assigned to the variable x using the assignment operator. The assignment operator is also used to assign an arithmetic expression to a variable. It is also capable of assigning value to multiple variables at a time.

Expression Assignment
x = y+7;
x = x-y+9; etc.

Multiple Assignment
x = y = z = 10;

Another Form Of Assignment Operator

While using the assignment operator in a C program it may be seen that the variable that is being used occurs on both sides of the assignment operator. In that case, the program statement can be written in the following ways.

Example: x = x+7; it can also be written as x +=7; it means the 7 will be assign to the variable ‘x’. Such a combination of the arithmetic operators with an assignment operators is collectively called a compound assignment operator. Here are the standard examples of compound assignment operators.

x = x+7 can also be written as x +=7;
x = x-7 can also be written as x -=7;
x = x*7 can also be written as x *=7;
x = x/7 can also be written as x /=7;
x = x%7 can also be written as x %=7;

Leave a Comment

Your email address will not be published. Required fields are marked *