Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Arithmetic Operators in C

When programming in the C language, the arithmetic operators help in performing mathematical functions. Arithmetic Operators in C allows a user to construct various formulas and mathematical equations. In this article, we will discuss the arithmetic operators according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents:

Types of Arithmetic Operators in C:

These are two types of Arithmetic Operators in C.

  • Binary
  • Unary

Binary Arithmetic Operators

This type of operator operates on two of the operands. A typical binary operator appears in this format with its operands:

operand1 operator operand2

Let us assume that variable A has a value of 5, and variable B has a value of 10. Then:

Operator Description Example
Subtraction of the second variable from the first one var = A – B = -5
+ Addition of two operands var = A + B = 15
/ Division of the numerator by the de-numerator var = B / A = 2
* Multiplication of both the operands var = A * B = 50
% The modulus operator and the remainder after the division of an integer var = B % A = 0

Unary Arithmetic Operators

This type of operator operates only on one operand. A typical unary operator appears in the following format with its operand:

operator operand

Let us assume that variable A has a value of 5, and variable B has a value of 10. Then:

Operator Description Example
Decrease of the integer value by the decrement operator by one var = A–
++ Increase of the integer value by the increment operator by one var = A++
Unary minus var = -A
+ Unary plus var = +A

Arithmetic Operators in Action

How to Perform Increment and Decrement in the C Programming Language

Here is a trick for such kinds of loops in your code: the decrement and increment operators. They are very useful in such cases.

We use the ++ for adding one to a value of a variable, like:

var++;

Once we execute this statement, the overall value of a var variable increases by 1 (increment). It’s similar to writing the code as following:

var = var + 1;

The decrementing operator — is the opposite of the ++ operator. Instead of a plus, here we have two minus signs. The decrement operator decreases the value of the available variable by one. For instance:

var–;

Once we execute this statement, the overall value of a var variable decreases by 1 (decrement). It’s similar to writing the code as following: var = var – 1;

One can find the ++ used a lot, especially in the loops, for instance:

for(x=0;x<100;x++)

The looping statement mentioned above gets repeated about 100 times. And it is way too cleaner than a user writing its alternative:

for(x=0;x<100;x=x+1)

How does one Prefix the — and ++ Operators?

The operator — always decrements the available value of the variable, while the ++ increments the value. Knowing this, we can consider the following statement:

var = A = B++;

In this case, if the value of the available variable is 17, then you must know that it will be 18 after we perform the ++ operation. So, what is the original value of the variable now- 17 or 18?

Generally speaking, we read the math equations of the C language from left to right. On the basis of this rule, the value of the variable preceding the execution of the statement is 17, and the current value of the variable is 18.

What comes first in operation — the = or the ++?

When we place the — or the ++ operator after a given variable, then it is known as post-decrementing and post-incrementing, respectively. In case we want to decrement or increment the given value of the variable before we use it, then we place the — or ++ before we place the name of the variable. For instance:

a = ++b;

Here, we can witness that there is an increment in the value of b, and then it gets assigned to the variable a.

Also, what if a complex condition occurs, such as:

a = ++b++;

We would get no output. It is because the ++var++ results in an error in the code. So we can ignore this condition.

How can one Discover the (Modulus) Remainder?

Out of all the basic symbols of the arithmetic operator, the % is the strangest. It is not an operator for the percentage- it is, rather, a modulus operator. This modulus operator calculates the remainder obtained by one number divided by the other.

Any modulus operation will display the remainder of the first value that gets divided by the second value. Thus, 21%5 will be 1, but 20% of 5 will be 0.

Examples of Arithmetic Operators

Go through the following example to understand how all the arithmetic operators function in the C program:

#include <stdio.h>

main() {

int p = 21;

int q = 10;

int r ;

r = p + q;

printf(“Line 1 – Value of r is %d\n”, r );

r = p – q;

printf(“Line 1 – Value of r is %d\n”, r );

r = p * q;

printf(“Line 1 – Value of r is %d\n”, r );

r = p / q;

printf(“Line 1 – Value of r is %d\n”, r );

r = p % q;

printf(“Line 1 – Value of r is %d\n”, r );

r = p++;

printf(“Line 1 – Value of r is %d\n”, r );

r = p–;

printf(“Line 1 – Value of r is %d\n”, r );

}

Once you have completed compiling and executing the program mentioned above, it will produce the results as follows:

Line 1 – The value of r is 31

Line 2 – The value of r is 11

Line 3 – The value of r is 210

Line 4 – The value of r is 2

Line 5 – The value of r is 1

Line 6 – The value of r is 21

Line 7 – The value of r is 22

Practice Problems on Arithmetic Operators in C

1. Determine the precedence of the given arithmetic operators (highest to lowest).

a) %, +, -, *, /

b) +, -, %, *, /

c) %, +, /, *, –

d) %, *, /, +, –

Answer – d

2. Find out which of the following isn’t a type of arithmetic operation.

a) a % = 10;

b) a ! = 10;

c) a / = 10;

d) a * = 10;

Answer – a

3. Find out which data type out of the following will throw an error during the modulus operation(%).

a) float

b) int

c) short

d) char

Answer – a

4. Among the following, which ones are the very fundamental arithmetic operators? (i.e, we cannot perform the desired operation using that operator only)

a) +, -, *, /, %

b) +, -, *, /

c) +, -, %

d) +, –

Answer – d


FAQs on Arithmetic Operators in C

Q1

How are arithmetic operators different from the other operators in C?

We use the arithmetic operators for performing various mathematical operations on the available operands.

Q2

What are the types of arithmetic operators?

It has various binary and unary operators. The binary operators include +, -, /, *, and %, along with unary operators like ++ and –.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*