Increment and Decrement Operators                   www.eshikshak.co.in
Increment Operator (++) ● The increment operator (++) adds 1 to its operand. ● It is an unary operator  ( A operator that ...
Decrement Operator (--) ● The decrement operator (++) subtract 1 from its   operand. ● It is an unary operator   ( A opera...
Postfix and Prefix Forms ● There are two forms for each of the operators:   postfix and prefix ● The statement ++i (prefix...
Postfix Forms - Example ● i = 5;   j = i++;   printf(“nj : %d and i : %d”, j, i); ● Output :   j : 5 and I : 6            ...
Postfix Forms - Example ● i = 5;   j = i++;   printf(“nj : %d and i : %d”, j, i);   printf(“ni : %d”,i++);   printf(“ni : ...
Prefix Forms - Example ● i = 5;   j = ++i;   printf(“nj : %d and i : %d”, j, i); ● Output :   j : 6 and i : 6             ...
Prefix Forms - Example ● i = 5;   j = ++i;   printf(“nj : %d and i : %d”, j, i);   printf(“ni : %d”,++i);   printf(“ni : %...
Precedence of Increment andDecrement Operators ● The increment and decrement operators have   the same precedence, but bin...
Precedence of Increment andDecrement Operators -Examples the value of k in i then ● i=k--; /* Stores  decrements k. */ ● J...
www.eshikshak.co.in
Upcoming SlideShare
Loading in...5
×

Lecture 8 increment_and_decrement_operators

2,077

Published on

Programming C increment and decrement

Published in: Education, Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,077
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
44
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lecture 8 increment_and_decrement_operators

  1. 1. Increment and Decrement Operators www.eshikshak.co.in
  2. 2. Increment Operator (++) ● The increment operator (++) adds 1 to its operand. ● It is an unary operator ( A operator that requires only one operand is known as unary operator ) ● Auto-increment ● i++ add 1 to i ● i = i + 1 can be represented in two forms as 1) i += 1 2) i++ ● k = 2 * i++ ( means ``take is old value and multiply it by 2, increment i, store the result of the multiplication in k ) www.eshikshak.co.in
  3. 3. Decrement Operator (--) ● The decrement operator (++) subtract 1 from its operand. ● It is an unary operator ( A operator that requires only one operand is known as unary operator ) ● Auto-decrement ● i-- subtracts 1 from i ● i = i - 1 can be represented in two forms as 1) I -= 1 2) i-- ● k = 2 * i-- ( means ``take is old value and multiply it by 2, decrement i, store the result of the multiplication in k ) www.eshikshak.co.in
  4. 4. Postfix and Prefix Forms ● There are two forms for each of the operators: postfix and prefix ● The statement ++i (prefix form) increments i before using its value ● The statement i++ (postfix form) increments it after its value has been used. ● The postfix increment and decrement operators fetch the current value of the variable ● Store a copy of it in a temporary location. The compiler then increments or decrements the variable www.eshikshak.co.in
  5. 5. Postfix Forms - Example ● i = 5; j = i++; printf(“nj : %d and i : %d”, j, i); ● Output : j : 5 and I : 6 www.eshikshak.co.in
  6. 6. Postfix Forms - Example ● i = 5; j = i++; printf(“nj : %d and i : %d”, j, i); printf(“ni : %d”,i++); printf(“ni : %d”,i); ● Output : j : 5 and i : 6 i:6 i:7 www.eshikshak.co.in
  7. 7. Prefix Forms - Example ● i = 5; j = ++i; printf(“nj : %d and i : %d”, j, i); ● Output : j : 6 and i : 6 www.eshikshak.co.in
  8. 8. Prefix Forms - Example ● i = 5; j = ++i; printf(“nj : %d and i : %d”, j, i); printf(“ni : %d”,++i); printf(“ni : %d”,i); ● Output : j : 6 and i : 6 i:7 i:7 www.eshikshak.co.in
  9. 9. Precedence of Increment andDecrement Operators ● The increment and decrement operators have the same precedence, but bind from right to left. So the expression ● --j++s evaluated as--(j++) www.eshikshak.co.in
  10. 10. Precedence of Increment andDecrement Operators -Examples the value of k in i then ● i=k--; /* Stores decrements k. */ ● J=i++; /* Stores the value of l in j then increments i. */ ● i=--k; /* Decrements k then stores the new value of k in i. */ ● j=++l; /* Increments l then stores the new value of l in j. */ www.eshikshak.co.in
  11. 11. www.eshikshak.co.in
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×