![](./write-blog/images/150935720-arithmetic-operators-in-c-programming-language.png)
Arithmetic Operators in C: Usage and Examples
☰Fullscreen
Table of Content:
- The basic arithmetic operations in C Programming are addition, subtraction, multiplication, and division.
- Arithmetic Operations are operated on Numeric Data Types as expected.
- Arithmetic Operators are “Binary” Operators i.e they operate on two operands. These operators are used in mathematical expressions in the same way that they are used in algebra.
The following table shows all the arithmetic operators supported by the C language.
Operator | Description | Example |
---|---|---|
+ (Addition) | Adds two operands | 5 + 10 =15 |
- (Subtraction) | Subtract second operands from first. Also used to Concatenate two strings | 10 - 5 =5 |
* (Multiplication) | Multiplies values on either side of the operator. | 10 * 5 =50 |
/ (Division) | Divides left-hand operand by right-hand operand. | 10 / 5 =2 |
% (Modulus) | Divides left-hand operand by right-hand operand and returns remainder. | 5 % 2 =1 |
++ (Increment) | Increases the value of operand by 1. | 2++ gives 3 |
-- (Decrement) | Decreases the value of operand by 1. | 3-- gives 2 |
Program:
#include void main() { int answer = 2 + 2; printf("Answer: %d \n",answer); answer = answer - 1; printf("Answer: %d \n",answer); answer = answer * 2; printf("Answer: %d \n",answer); answer = answer / 2; printf("Answer: %d \n",answer); answer = answer + 8; printf("Answer: %d \n",answer); answer = answer % 7; printf("Answer: %d \n",answer); }
Output:
Answer: 4 Answer: 3 Answer: 6 Answer: 3 Answer: 11 Answer: 4 Press any key to continue . . .
Increment and Decrement Operator
Program:
#include void main() { int a = 2 ; int b = 6 ; a++ ; printf("%d \n",a); b-- ; printf("%d \n",b); }
Output:
3 5 Press any key to continue . . .
Increment and Decrement Operator another example:
Program:
#include void main() { int x= 5; printf("%d\n",x++); printf("%d\n",++x); printf("%d\n",x--); printf("%d\n",--x); }
Output:
5 7 7 5 Press any key to continue . . .
Increment and Decrement Operator another example:
This is a undefined Behaviour of C programming Language.
Program:
#include void main() { int p=10; int q=10; printf("%d \n",p++ + ++p); printf("%d \n",q++ + q++); }
Output:
You may get this answer. But this is a undefined behaviour of C
22 20 Press any key to continue . . .
Below program also a undefined behaviour of C
#include int main() { int i = 8; int p = i++*i++; printf("%d\n", p); }
Use of Modulus Operator
Program:
#include void main() { int R = 42; int S = 62; printf("R mod 10 = %d \n",(R%10)); printf("S mod 10 = %d \n",(S%10)); }
Output:
R mod 10 = 2 S mod 10 = 2 Press any key to continue . . .
Pre increment and post increment with Example:
- Question 1: S++ or S = S+1, which can be recommended to increment the value by 1 and why?
- Question 2: What is difference between i++ and ++i?
- Question 3: Where the address of operator (&) cannot be used for?
- Question 4: What is reminder for 5.0 % 2?
- Question 5: How do you construct an increment statement or decrement statement in C?
- Question 6: What is the modulus operator?
- Question 7: What is arithmetic operator in C?
Related Questions
- Assignment 1: Program to show swap of two no's without using third variable
- Assignment 2: write a program that performs an arithmetic sequence summation (arithmetic series).
- Assignment 3: Write a program to demonstrate the results obtained by using the arithmetic operators for addition, subtraction, multiplication and division on integer data.
- Assignment 4: Write a program to demonstrate the results obtained by using the arithmetic operators on integer and floating point number.
- Assignment 5: Arithmetic Operations example in c programming langauage
- Assignment 6: Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
- Assignment 7: If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.