# Arithmetic Operators in C Programming

08 Jun 2024
Beginner
83 Views
Learn via Video Course & by Doing Hands-on Labs

## 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.

 Type Operator Name of Operator Functionality Unary ++ Increment Increases the value by 1 - - Decrement Decreases the value by 1 + Unary plus No change in the operand value - Unary minus changes the negative number to the positive and vice-versa Binary + Addition Add two values - Subtraction Subtracts one value from the other * Multiplication Multiplies two values / Division Divides one by the other value % Modulus Finds the remainder after division

## Flowchart of Arithmetic Operators in C

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

### 1. Binary Arithmetic Operators

Binary arithmetic operators require two operands to perform calculations. The main binary operators in C include:
• 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;
}
``````

• 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

### Q1. What are operators in C?

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

### Q3. What is %d in C?

%d is used to represent a signed decimal integer.
Share Article

### Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
 Azure Developer Certification Training Jun 29 SAT, SUN Filling Fast 07:00AM to 09:00AM (IST) .NET Microservices Certification Training Jun 29 SAT, SUN Filling Fast 05:30PM to 07:30PM (IST) React JS Certification Training | Best React Training Course Jun 30 SAT, SUN Filling Fast 08:30PM to 10:30PM (IST) ASP.NET Core Certification Training Jun 30 SAT, SUN Filling Fast 10:00AM to 12:00PM (IST) Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN Filling Fast 11:00AM to 01:00PM (IST) Advanced Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN Filling Fast 10:00AM to 12:00PM (IST) Generative AI For Software Developers Jul 14 SAT, SUN Filling Fast 08:30PM to 10:30PM (IST)

Can't find convenient schedule? Let us know

Similar Articles