Welcome to SPOTTURNS page, Greetings from my side with following link GATE HAND WRITTEN NOTES.

Friday, 1 May 2015

Multiple increment operators inside printf (VERY IMPORTANT )

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 :
spotturns
spotturns-program explination

Pictorial representation
Explanation of program
  1. 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.
  1. As shown in the above image evaluation sequence of expressions written inside printf will be – i++,++i,i
  1. After execution we need to replace the output of expression at appropriate place
NoStepExplanation
1Evaluate i++At the time of execution we will be using older value of i = 1
2Evaluate ++iAt the time of execution we will be increment value already modified after step 1 i.e i = 3
2Evaluate iAt 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 typeOperatorDescription
1Pre increment++i
Value of i is incremented before assigning it to variable i.
2Post-incrementi++
Value of i is incremented after assigning it to variable i.
3Pre decrement– –i
Value of i is decremented before assigning it to variable i.
4Post_decrementi– –
Value of i is decremented after assigning it to variable i.