Arithmetic Operators In C [ Full Information With Examples ]
Today we will learn in detail what is the Arithmetic Operators in C? and how to use Arithmetic Operators in C Language.
What is Arithmetic Operators In C
Arithmetic operators in C language are used to perform Arithmetic/ Mathematical operations such as Addition, Subtraction, Multiplication, Division, etc.
Arithmetic Operators are of two types -:
- Unary Operators
- Binary Operators
Unary Operators In C
Operators who need only one operand to perform the operation are called Unary Operators. Examples of this operators are -: ‘+ +’, ‘- -‘, here we call the “+ +” operator as the Increment Operator and the “- -” operator we call the Decrement Operator.
# 1) Increment Operator In C
Increment Operators are operators that, when used with a variable, then it increases the value of that variable by one.
For Example -: Suppose we have created a variable x by int data type and store 5 values in it. Now if we use Increment Operator in variable x then the value of x variable will increase from 5 to 6.
Let’s understand this by making a program.
#include <stdio.h>
int main ()
{
int x = 5;
printf("Before using increment operator the value of x is: %d", x);
++x;
printf("\n After using increment operator the value of x is: %d", x);
return 0;
}
Output -:
Before using increment operator the value of x is: 5
After using increment operator the value of x is: 6
We can use the Increment Operator in two ways, one before the Variable name and the other after the Variable name.
When we use increment operator before a variable name then it is called pre increment operator and when it is used after a variable name then it is called post increment operator.
While using the increment operator both ways, the value of the variable increases by one,
But when we use pre increment and post increment operator at a time where more than one operator is being used, then the result of both types of increment operator can be different there.
Let’s understand this with an example.
First we will create a program using the pre increment operator, then the second one using the post increment operator, and finally, we will compare the result/output of these two.
Example 1 – Using Pre Increment Operator
#include <stdio.h>
int main ()
{
int x = 5;
int y;
y = ++x; // pre increment operator
printf ("\n Pre Increment Operator Result -: Value is: x = %d, y = %d", x, y);
return 0;
}
Output -:
Pre Increment Operator Result -: Value is: x = 6, y = 6
Example 2 – Using Post Increment Operator
#include <stdio.h>
int main ()
{
int x = 5;
int y;
y = x++; // post increment operator
printf ("\n Post Increment Operator Result -: Value is: x = %d, y = %d", x, y);
return 0;
}
Output -:
Post Increment Operator Result -: Value is: x = 6, y = 5
In example number 1 we have used Pre Increment Operator, in which the result was x = 6 and y = 6 and in Example number 2 we have used Post Increment Operator, which results was x = 6 and y = 5. Think, we have made similar programs in both of them, but why did the results come differently?
Actually in Example 1, y = ++ x; When we did and used the Pre Increment Operator, the first variable was incremented in x and then in y assigned that value and In example 2, y = x++; When we did and used the Post Increment Operator, first the variable y was assigned a value of x, followed by an increment in the value of x, which led to x = 6 and y = 5 in the result.
So here it is to be noted that when more than one operator is using at the same time, the Operator Precedence Rule is followed and in these Examples, the Operator Precedence Rules were followed, due to which the result of both of them changed.
The priority of the Pre Increment Operator is higher than the assignment operator and the priority of the Post Increment Operator is less than the assignment operator.
We will read about Operator Precedence from details in any of our other posts. Right now we learn about what is the Decrement operator and how it is used in C language
# 2) Decrement Operator In C
Decrement operator is an operator that, when used with a variable, then it reduce the value of that variable by one. We can also use Decrement Operator as an increment operator in two ways, one before the variable name and the other after the variable name.
When we use Decrement Operator before variable name then it is called Pre Decrement Operator and when we use it after variable name then it is called Post Decrement Operator.
Example Program -: Pre Decrement Operator In C Language
#include <stdio.h>
int main ()
{
int x = 5;
int y;
y = --x; // pre Decrement Operator
printf ("\n Pre Decrement Operator Result -: Value is: x = %d, y = %d", x, y);
return 0;
}
Output -:
Pre Decrement Operator Result -: Value is: x = 4, y = 4
Example Program -: Post Decrement Operator Example
#include <stdio.h>
int main ()
{
int x = 5;
int y;
y = x--; // post Decrement Operator
printf ("\n Post Decrement Operator Result -: Value is: x = %d, y = %d", x, y);
return 0;
}
Output -:
Post Decrement Operator Result -: Value is: x = 4, y = 5
Binary Operators In C
The operators who require two operands to perform the operation are called Binary Operators.
Examples of this Operators are: (+, -, *, /,%). here “+” Is called an Addition Operator, “-” is called Subtraction Operator. “*” Is called a Multiplication Operator. “/” Is called Division Operator. “%” Is called Modulus Operator or Modulo Operator.
Let’s now learn about all these operators one by one -:
- Addition Operator (+) -: It works by connecting two operands Example -: a + b.
- Subtraction Operator (-) -: It works by subtracting two operands Example -: a-b
- Multiplication Operator (*) -: It works by multiplying two operands Example -: a * b
- Division Operator (/) -: It works to David from the first operand to the second operand. Example -: a / b
- Modulus Operator (%) -: Returns the remainder when the second operands are divided by the first example -: a% b
Example
#include <stdio.h>
int main ()
{
int a, b, c;
printf ("Enter two number \n");
scanf ("%d %d", &a, &b);
c = a+b;
printf ("Addition of a + b is: %d", c);
c = a-b;
printf ("\n Subtraction of a-b is: %d", c);
c = a*b;
printf ("\n Multiplication of a*b is: %d", c);
c = a/b;
printf ("\n Division of a/b is: %d", c);
c = a%b;
printf ("\n Remainder of is: %d", c);
return 0;
}
Output -:
Enter two number
10
5
Addition of a + b is: 15
Subtraction of a-b is: 5
Multiplication of a*b is: 50
Division of a/b is: 2
Remainder of is: 0
Read More -:
- What is Operators In C
- Relational Operators In C
- Logical Operators In C
- Bitwise Operators In C
- Assignment Operators In C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope you have found the answer of your question and you will not have to search about the Arithmetic Operators in C language.
However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Arithmetic Operators in C Language.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.