Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Increment and Decrement Operators in C Language with GATE 2019 Question

4 min 15 sec

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 

 

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor