• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

Arithmetic Operators

Contents

  • 1 Basic Arithmetic Operators
    • 1.1 Arithmetic Operator Precedence
  • 2 Modulus Operator
  • 3 Arithmetic Assignment Operator
  • 4 Increment Operator
    • 4.1 Prefix
    • 4.2 Postfix
  • 5 Decrement Operator

To perform arithmetic operations such as addition, subtraction, multiplication and division, C language provides you with arithmetic operators such as +, -, * and / respectively. These arithmetic operations can be performed on integer and floating-point data types.

c = a + b;

In the above statement, there are 2 operations that are happening. First operation is arithmetic operation, addition of a and b, second operation is assignment operation, sum of a and b is assigned to c.

Every operation comprises an operator and 2 operands. Operator is the symbol that signify the operation, and operand is the variable or constant upon which the operation is performed. In the above example, a and b are the operands and + is the operator.

Basic Arithmetic Operators

These are the operators we are very much familiar with in our day to day activities.

OperationOperatorExample
Addition+2 + 2
Subtraction–5 – 2
Multiplication*2 * 2
Division/8 / 2

Arithmetic Operator Precedence

If an expression consists of multiple operations, the order of evaluation of those operations would be as below:

OrderOperators
1* / %
2+ –
3=

What would be the output of the below program?

#include <stdio.h>

void main()
{
    int n = 2 + 8 / 2 * 2 * 5 % 2;
    printf("%d", n);
}

In this expression, multiplication, division and modulus operations will be executed as per their order in the expression. First 8/2 evaluates to 4, then 4*2*5 evaluates to 40, then 40%2 evaluates to 0, finally 2 + 0 evaluates to 2.

Modulus Operator

Apart from the basic arithmetic operators, we also have a modulus operator that only work with integer type variables and constants. This operator can be used for finding the reminder of a division. This operator is also called reminder operator.

#include <stdio.h>

void main()
{
    int a = 5, b = 2;
    int c = a % b;
    printf("Reminder of %d / %d is %d", a, b, c);
}

Arithmetic Assignment Operator

Arithmetic Assignment Operators are used when you want to perform an operation on an operand and store it in the operand itself. It combines the arithmetic operator and assignment operator and eliminates the repetition of the operand in an expression.

The usual way of writing such an expression would be, for example:

int a = 10;
a = a + 20;

Using an Arithmetic Assignment Operator, you can shorten this expression as follows:

int a = 10;
a += 20;

C language provides arithmetic assignment operators for all arithmetic operations.

ExpressionUsing Arithmetic Assignment Operator
a = a + 2a += 2
a = a – 2a -= 2
a = a * 2a *= 2
a = a / 2a /= 2
a = a % 2a %= 2

Increment Operator

Using an increment operator we can increment a variable value by 1.

int a = 10;
a++; // a becomes 11
++a; // a becomes 12

As you see in the above code snippet it really doesn’t matter where you put the increment operator when you write them as an individual statement.

However, it will have an impact when you use them as a part of an expression.

Prefix

When you write increment operator before the variable, it is called prefix. In prefix operation, the increment will happen before its value is used in an operation.

int a = 10, b = 5, c;
c = b + ++a; // c value will be 16

Postfix

When you write increment operator after the variable, it is called postfix. In postfix operation, the increment happens after its value is used in an operation.

int a = 10, b = 5, c;
c = b + a++; // c value will be 15

Decrement Operator

Similar to increment operator, we also have decrement operator which decrements a variable by 1.

We also have pre and postfix decrement variables, which act similar to the increment variable.

int a = 10, b = 5, c;
c = b + a--; // c value will be 15
c = b + --a; // c value will be 14

Primary Sidebar

More to See

Arrays.asList in Java Examples

February 21, 2021 By Admin

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 By Admin

Secondary Sidebar

  • C Tutorial
    • First C Program
    • Variables
    • Data Types
    • Arithmetic Operators
    • Relational Operators
    • if else statement

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • Misc
  • Node.js
  • Spring Core
  • Windows

Archives

  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

Navigation

  • Home
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc

Recent

  • How to Make File Explorer Open to This PC instead of Quick Access in Windows 10
  • Arrays.asList in Java Examples
  • [Solved] Why List.add throws UnsupportedOperationException in Java?
  • How to Convert an Array to List in Java?
  • How Many Spaces in a Tab?

Search

Copyright © 2021 · Tech Stack Journal · Log in