position: fixed; top: auto !important; margin-left: 112px;

Increment & Decrement Operators

How Increment and Decrement Operators performs in case of postfix and prefix is what you will learn in this tutorial. Let's dive deep into what actually is postfix and prefix in C language.

Postfix Increment

int i = 10;
printf("%d", i++);
printf(“\n%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(“\n%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(“\n%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(“\n%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 



Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.