Arithmetic Operators in C Programming Language

Rumman Ansari   Software Engineer   2022-07-19   11631 Share
☰ Table of Contents

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<stdio.h>
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<stdio.h>
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<stdio.h>
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<stdio.h>
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 <stdio.h> 
int main() 
{ 
   int i = 8; 
   int p = i++*i++; 
   printf("%d\n", p); 
}

Use of Modulus Operator

Program:


#include<stdio.h>
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: