Multiple increment operators inside printf
#include<stdio.h> void main()
{
int i = 1;
printf("%d %d %d", i, ++i, i++);}
output:
3 3 1
Output :
Pictorial representationExplanation of program
- Whenever more than one format specifiers (i.e %d) are directly or indirectly related with same variable (i,i++,++i) then we need to evaluate each individual expression from right to left.
- As shown in the above image evaluation sequence of expressions written inside printf will be – i++,++i,i
- After execution we need to replace the output of expression at appropriate place
No | Step | Explanation |
---|---|---|
1 | Evaluate i++ | At the time of execution we will be using older value of i = 1 |
2 | Evaluate ++i | At the time of execution we will be increment value already modified after step 1 i.e i = 3 |
2 | Evaluate i | At the time of execution we will be using value of i modified in step 2 |
I am sure you will get confused after viewing the above image and output of program.
MY fb PROGRAM:
#include <stdio.h>
#include <conio.h>
int main()
{
int a=5;
printf("%d %d %d", a++,a,a--);
}
guass the output to above program????????????????????????????
OUTPUT:
4 4 5
Different Types of Increment Operation
In C Programming we have two types of increment operator i.e Pre-Increment and Post-Increment Operator.
A. Pre Increment Operator
Pre-increment operator is used to increment the value of variable before using in the expression. In the Pre-Increment value is first incremented and then used inside the expression.
b = ++y;
In this example suppose the value of variable ‘y’ is 5 then value of variable ‘b’ will be 6 because the value of ‘y’ gets modified before using it in a expression.
B. Post Increment Operator
Post-increment operator is used to increment the value of variable as soon as after executing expression completely in which post increment is used. In the Post-Increment value is first used in a expression and then incremented.
b = x++;
Example program for increment operators in C:
- In this program, value of “i” is incremented one by one from 1 up to 9 using “i++” operator and output is displayed as “1 2 3 4 5 6 7 8 9”.
//Example for increment operators
#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
Output:
1 2 3 4 5 6 7 8 9 |
Example program for decrement operators in C:
- In this program, value of “I” is decremented one by one from 20 up to 11 using “i–” operator and output is displayed as “20 19 18 17 16 15 14 13 12 11”.
//Example for decrement operators
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
{
printf("%d ",i);
i--;
}
}
Output:
20 19 18 17 16 15 14 13 12 11 |
Difference between pre/post increment & decrement operators in C:
- Below table will explain the difference between pre/post increment and decrement operators in C.
S.no
| Operator type | Operator | Description |
1 | Pre increment | ++i | Value of i is incremented before assigning it to variable i. |
2 | Post-increment | i++ | Value of i is incremented after assigning it to variable i. |
3 | Pre decrement | – –i | Value of i is decremented before assigning it to variable i. |
4 | Post_decrement | i– – | Value of i is decremented after assigning it to variable i. |
4 comments:
The blog is good.I got a more knowledge from this article.Thanks.
javascript training in chennai
javascript training in Porur
core java training in chennai
core javaTraining in Tambaram
C++ Training in Chennai
C C++ Training in OMR
core java training in chennai
core java Training in Anna Nagar
I'd like to thank you for the efforts you have put in writing this blog.thank you so much.
C and C++ Training Institute in chennai | C and C++ Training Institute in anna nagar | C and C++ Training Institute in omr | C and C++ Training Institute in porur | C and C++ Training Institute in tambaram | C and C++ Training Institute in velachery
Marmoset Toolbag Crack suggestions a swing of exhilarating original landscapes a fresh typical in double superiority innovative animatronics tackles besides .Marmoset Toolbag 4.0.5.3 Crack
Kindly Visit my website for cracked software, Windows & Mac. Download Now Latest Versions.
Avast Cleanup Premium Crack
Post a Comment