In this article, you'll learn the increment operator ++ and therefore the decrement operator -- intimately with the assistance of examples.
Increment and decrement operators are unary operators that add or subtract one, to or from their operand, respectively.
- In implementation when we need to increment the value of the variable by 1 then go for increment or decrement operators i.e. ++,--
- When we are working with increment/decrement operators then modification between old value and new value will be +1/-1.
- Increment/Decrement operators are of two types:
- Pre operators.
- Post operators.
- When the symbol is available before the operands then it is called pre operator, if the symbol is available after the operand then it is called post operator.
- When we are working with the pre operators before evaluating the expressions the value need to be change i.e modification first, substitution later.
- When we are working with the post operators after evaluating the expressions the value need to be change i.e substitution first, modification later.
#include <stdio.h>
int main() {
int a, b;
a=1;
//Syntex 1: (pre incr)
b = ++a;
//first increment the value of a by 1, then evaluate the expression i.e. b=a;
printf("%d %d", a,b); //O/P: a = 2, b=2
//Syntex 2: (post incr)
b = a++;
//first evaluate the expression i.e. b=a, then increment the value of a by 1
printf("%d %d", a,b); //O/P: a = 2, b=1
//Syntex 3: (pre decr)
b = --a;
//first decrement the value of a by 1, then evaluate the expression i.e. b=a;
printf("%d %d", a,b); //O/P: a = 0, b=0
//Syntex 4: (post decr)
b = a--;
//first evaluate the expression i.e. b=a, then decrement the value of a by 1
printf("%d %d", a,b); //O/P: a = 0, b=1
return 0;
}
Pre-Increment:
#include <stdio.h> void main () { int a; a = 10; ++a; //a=a+1 printf ("%d", a); //O/P: a = 11 }
Post-Increment:
#include <stdio.h> void main () { int a; a = 10; a++; //a=a+1 printf ("%d", a); //O/P: a = 11 }
Until we are not assigning the data to any other variable, there is no difference between pre and post operators.
Operators | Description | Associativity |
---|---|---|
() | parentheses | left to right |
++ , -- | postfix increment operator, postfix decrement operator | left to right |
++ , -- , + , - | prefix increment operator, prefix decrement operator, unary plus, unary minus | right to left |
* , / , % | Multiplication, Division and Modulus | left to right |
+ , - | Addition and Subtraction | left to right |
= , += , -= , *= , /= , %= | Assignment Operator and Compound assignment operator | right to left |
Type 1: Program1-
#include <stdio.h> void main () { int a; a = 1; a = ++a + ++a + ++a; printf ("%d", a); //O/P: a = 12 }
O/P: a = ++a + ++a + ++a = 4 a = 1/,2/,3/,4
a = a + a + a
a = 4 + 4 + 4
a = 12
Program 2-
#include <stdio.h>
void
main ()
{
int a;
a = 1;
a = ++a + a++ + ++a;
printf ("%d", a);
}
Program 3-
Type 2: Program1-
#include <stdio.h> void main () { int a, b; a = b = 50; a = a++ + ++b; b = b++ + ++a; printf ("a=%d b=%d", a,b); //O/P: a = 103 b = 155 } Explanation- a = a++ + ++b a = 50 + 51 a = 101 // a++ = 102 b = b++ + ++a //a increment first 102+1 b = b + a b = 51 + 103 = 154 //b is post increment so b is finally 155 //O/P: a = 103 b = 155
Program 2-
Type 3: Program1-
- Printf is a predefined function which is use it to print the data on the console.
- printf function will works with the help of stack i.e. LIFO concept.
- When we are working with the printf function always arguments should be passed towards from R->L data should be printed towards from L->R.
Program 3-
- When we are working with any expression it can be evaluated in two location i.e.
- Register evaluation
- Stack evaluation
- When we are working with register evaluation it work with the help of priority i.e pre operator are contain highest priority then post operator.
- In register evaluation data need to be substitute after modified all pre value.
- When we are working with stack evaluation pre post operator contain same priority.
- In stack evaluation data need to be substituted at the time of evaluating the expression.
- When the expression is passing out side of the printf then it works with the help of register with in the printf stack evaluation.
- Whenever we are passing more then one argument in to the printf statement then go for right -> left, if we having only one argument then data should be substituted towards from left to right.
Program1-
#include <stdio.h> void main () { int a,b; a = 1; b = ++a * ++a * ++a printf ("a=%d b=%d", a,b); //O/P: a = 4 b = 64 } Explanation- b = ++a * ++a * ++a = a * a * a = 4 * 4 * 4 = 64
Program2-
#include <stdio.h> void main () { int a,b; a = 1; b = ++a * a++ * ++a printf ("a=%d b=%d", a,b); //O/P: a = 4 b = 27 } Explanation- b = ++a * a++ * ++a // first a has value 1 then ++a =2, ++a = 3 //last a++ increment value is 4 = a * a * a = 3 * 3 * 3 = 27
Program3-
#include <stdio.h> void main () { int a,b; a = 1; b = a++ * a++ * a++; printf ("a=%d b=%d", a,b); //O/P: a = 4 b = 1 } Explanation- b = a++ * a++ * a++ // first a has value 1 first evaluate the expression then increment value //last a++ increment 3 times value is 4 = a * a * a = 1 * 1 * 1 = 1
Stack evaluation
Program-
Program-
#include <stdio.h>
void
main ()
{
int a,b;
a = 1;
printf ("%d", ++a * ++a * ++a); // 2 * 3 * 4 = 24
a = 1;
printf ("%d", ++a * a++ * ++a); // 2 * 2 * 4 = 16
a = 1;
printf ("%d", a++ * ++a * a++); // 1 * 3 * 3 = 9
a = 1;
printf ("%d", a++ * a++ * a++); // 1 * 2 * 3 = 6
a = 1;
printf ("%d", a++ * ++a * ++a); // 1 * 3 * 4 = 12
}
I hope this article was helpful to you. So, please share it together with your friends and colleagues using the social buttons below!
0 Comments