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 

 

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python