Arithmetic Operators in C Programming

Arithmetic Operators in C Programming

08 Jun 2024
Beginner
2.9K Views
12 min read
Learn with an interactive course and practical hands-on labs

Free C Foundation Course: Learn C In 21 Days

Arithmetic Operators in C

Arithmetic Operators are used in various expressions to perform functions such, as addition, subtraction, multiplication, division, and calculating the remainder. These Operators are essential, for managing data and performing calculations in C programs.

In this C Tutorial, we will see in-depth information about arithmetic operators including arithmetic operators in c programming with examples

What are C Arithmetic Operators?

  • In C programming Arithmetic Operators play a role, in carrying out tasks within a program.
  • These operators help define expressions and mathematical calculations using symbols to perform operations on operands.
  • C offers a total of 9 operators enabling users to execute arithmetic functions, like addition subtraction, multiplication, and more.

Syntax

In C, arithmetic operators have a simple syntax. To compute a result, they operate in between operands, which are variables or constants. The basic syntax for employing arithmetic operators is as follows:

   result = operand1 operator operand2;   

Example

int result;
result = 5 + 3;  // Using the addition operator   

Types of Arithmetic Operators in C

C provides several arithmetic operators, each serving a specific purpose.
  1. Binary operators.
  2. Unary operators.

These operators perform basic mathematical operations like addition, subtraction, multiplication, and division. We will categorize the arithmetic operators into two categories based on the number of operands and then look at their functionality.

TypeOperatorName of OperatorFunctionality
Unary++IncrementIncreases the value by 1
- -DecrementDecreases the value by 1
+Unary plusNo change in the operand value
-Unary minuschanges the negative number to the positive and vice-versa
Binary+AdditionAdd two values
-SubtractionSubtracts one value from the other
*MultiplicationMultiplies two values
/DivisionDivides one by the other value
%ModulusFinds the remainder after division

Flowchart of Arithmetic Operators in C

To visualize how arithmetic operations flow in a C program, consider the following flowchart:
                                               Flowchart of Arithmetic Operators in C

1. Binary Arithmetic Operators

Binary arithmetic operators require two operands to perform calculations. The main binary operators in C include:
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

Example

Let's see an example of a Binary Arithmetic Operator in C in C Compiler.
   
#include <stdio.h>
int main()
{
    int x = 25, y = 5, operation;
    printf("if x is %d and y is %d\n", x, y);
 
    operation = x + y; // addition Operation
    printf("Addition of x + y is %d\n", operation);
 
    operation = x - y; // subtraction
    printf("Substraction of x - y is %d\n", operation);
 
    operation = x * y; // multiplication
    printf("Multiplication of x * y is %d\n", operation);
 
    operation = x / y; // division
    printf("Division of x / y is %d\n", operation);
 
    operation = x % y; // modulus
    printf("Modulas of x %% y is %d\n", operation);
 
    return 0;
}
    

Output

if x is 25 and y is 5

Addition of x + y is 30
Substraction of x - y is 20
Multiplication of x * y is 125
Division of x / y is 5
Modulas of x % y is 0    

2. Unary Arithmetic Operators

Unary arithmetic operators work with a single operand. The primary unary operators in C are:
  • + (Unary plus)
  • - (Unary minus)
  • ++ (Increment)
  • -- (Decrement)

Example

Here’s an example elaborating the use of unary arithmetic operators in C:
#include <stdio.h>

int main()
{
	int x = 91, y = 99, Operation;

	printf("Post Increment and Decrement\n");
	
	Operation = x++;
	printf("x is %d and result is %d\n", x,
		Operation); // x becomes 91 now

	// post-decrement example:
	// Operation is assigned 91 only, x is not updated yet
	Operation = x--;
	printf("x is %d and result is %d\n", x,
		Operation); 

	printf("\nPre Increment and Decrement\n");
	Operation = ++x;

	printf("x is %d and result is %d\n", x, Operation);
	
	Operation = --x;

	// a and Operation have same values = 10
	printf("x is %d and result is %d\n", x, Operation);

	return 0;
}
    

Output

   
Post Increment and Decrement
x is 92 and result is 91
x is 91 and result is 92

Pre Increment and Decrement
x is 92 and result is 92
x is 91 and result is 91
    

Multiple Operators in a Single Expression

C allows the use of multiple arithmetic operators in a single expression. Operator precedence determines the order of operations.
For example:
   int result = a + b * c - d / e;   
In this expression, multiplication and division are performed before addition and subtraction due to their higher precedence.

Examples of C Arithmetic Operators

Here are more examples illustrating arithmetic operations in C:

#include <stdio.h>
int main()
{
 //unary operators
 int x = 5, y=6, z=7, w=8;
 printf("%d\n", ++x); //increments the value of x
 printf("%d\n", --y); //decrements the value of x
 printf("%d\n", +z); // unary +
 printf("%d\n", -w); // unary - on a positive number
 printf("%d\n",-(-w)); // unary - on a negative number
 // binary operators
 int a = 10, b = 3;
 int sum = a + b;
 int difference = a - b;
 int product = a * b;
 int quotient = a / b;
 int remainder = a % b;
 printf("Sum: %d\n", sum);
 printf("Difference: %d\n", difference);
 printf("Product: %d\n", product);
 printf("Quotient: %d\n", quotient);
 printf("Remainder: %d\n", remainder);
 return 0;
}

In the above code in the C Compiler, we have performed all the arithmetic operations on unary as well as binary operands.

Output

6
5
7
-8
8
Sum: 13
Difference: 7
Product: 30
Quotient: 3
Remainder: 1

Arithmetic Operations with Char Data Type In C

arithmetic operations can also be performed on char data types, as they are essentially small integers. For example:
   
#include <stdio.h>

int main() {
    char a = 'A';
    char b = 2;
    char result = a + b;

    printf("Result: %c\n", result); // Outputs 'C'

    return 0;
}
    

Advantages of Arithmetic Operators

  • Ease; They offer a method to carry out math computations.
  • Speed; Math symbols are finely tuned for quick execution.
  • Versatility; They work well with data formats such, as numbers, decimal numbers, and letters.
Conclusion:

Understanding the syntax, types, and examples of arithmetic operators, in C is crucial, for performing mathematical operations and manipulating data. This knowledge enables you to create optimized C programs. Also, Master your coding base in C programming to the next level by pursuing our C Programming Course.

Similar Articles of C
C Programming Assignment Operators
Operators in C: Types of Operators
Bitwise Operators in C: AND, OR, XOR, Shift & Complement
Interview Article of C
Top 50 Most Asked C Interview Questions and Answers

FAQs

The operators are types of symbols that inform a compiler for performing some specific logical or mathematical functions

 %d is used to represent a signed decimal integer.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this