Arithmetic Operators in C Language
Operator in C Language
For example: in a + b, the + symbol is an operator
(and a and b are called operands).
The C language provides different types of operators to perform different operations.
In this topic, we will learn about the basic operators: arithmetic operators and assignment operators. We will learn about the remaining operators in later topics.
Arithmetic Operators in C Language
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (Remainder) | a % b |
Example program to demonstrate basic arithmetic operations in C Language
#include <stdio.h>
int main()
{
int a = 15, b = 5, c;
c = a + b;
printf("a+b = %d \n", c);
c = a - b;
printf("a-b = %d \n", c);
c = a * b;
printf("a*b = %d \n", c);
c = a / b;
printf("a/b = %d \n", c);
c = a % b;
printf("a%b = %d \n", c);
return 0;
}
Output:
a+b = 20
a-b = 10
a*b = 75
a/b = 3
a%b = 0
Here, the % operator is called the modulus operator.
It returns the remainder when a is divided by b.
Assignment Operators in C Language
= is called the assignment operator.
It is shown below.
Along with =, C also provides shorthand assignment operators.
We will learn about them later.
| Operator | Description | Example |
|---|---|---|
| = | Assigns the value from the right-hand side to the left-hand side | a = b |
Example program demonstrating the assignment operator in C Language
#include <stdio.h>
int main()
{
int a, b;
a = 10;
b = 20;
printf("a = %d \n", a);
printf("b = %d \n", b);
return 0;
}
Output:
a = 10
b = 20
Here, we used the = operator to assign the value 10 to variable a
and the value 20 to variable b.
So far, we have learned:
- What is an Operator?
- Arithmetic Operators
- Assignment Operator (=)
- We will learn the remaining operators in upcoming topics.