Increment and Decrement Operators in C Language with GATE 2019 Question

4 min 15 sec Basic


Postfix Increment

int i = 10;
printf("%d", i++);
printf(“
%d", i);

Output :-

10
11

Here,

i++ is postfix operator because ++ is used after a unary operand. Unary means one operand which is i in this given example. In postfix case , value is assigned to i at first which is 10 and then after that whenever i is being used its value will be incremented by 1 when ++ is used .

Prefix Increment

int i = 10;
printf("%d", ++i);
printf(“
%d", i);

Output :-

11
11

Here,

++i is prefix operator because ++ is used before a unary operand. Unary means one operand which is i in this given example. In prefix case, the value is incremented by 1 at first and then from that point value of i is being changed to incremented value.

Decrement Operator

Postfix Decrement

int i = 10;
printf("%d", i--);
printf(“
%d", i);

Output :-

10
9

Here,

i-- is postfix operator because -- is used after a unary operand. Unary means one operand which is i in this given example. In postfix case , value is assigned to i first which is 10 and then after that whenever i is being used its value will be decremented by 1 when -- is used.

Prefix Decrement :-

int i = 10;
printf("%d", --i);
printf(“
%d", i);

Output :-

9
9

Here,

--i is prefix operator because -- is used before a unary operand. Unary means one operand which is i in this given example. In prefix case , value is decremented by 1 and then from that point value of i is being changed to decremented value when -- is used .

Example

a = 10;
                                  Output
a++         =     10 ++           =  10
a++         =     11++            =  11
a++         =     12++            =  12 
a           =     13              =  13

++a         =     ++10            = 11
++a         =     ++11            = 12
++a         =     ++12            = 13
a           =      13             = 13  

As discussed,

Remember this rule

In postfix case, the value is assigned to i at first which is 10 and then after that whenever i is being used its value will be incremented by 1 when ++ is used .

a++ will use the original value which is 10 at first and then in the second step whenever a is used its value will be incremented so in second step value will become 11 which is then incremented in third step so its value becomes 12 in the third step which is then incremented so whenever its used in fourth step its value will be incremented by 1.

In prefix case, the value is incremented by 1 at first and then from that point value of i is being changed to incremented value.

++a increments the value directly and use the incremented value afterward.

Similarly in case of decrement operator.

Example


a = 10;
                                               Output
a--         =     10--           =  10
a--         =     9--            =  9
a--         =     8--            =  8
a           =     7              =  7

--a         =     --10           =  9
--a         =     --9            =  8
--a         =     --8            =  7
a           =       7            =  7 

 

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials