Close
Close

When I print the value of x=x++ there is no increment in the value of x. Why does this happen?

   Prathamesh Bagul , IIT Kanpur

#include <stdio.h>
int main( )
{
    int x = 10, y = 3, z;
    for(z = 0; z<x; )
    {
    z = z++ +y;
    printf("%d\n", z) ;
    }
    return 0;
}

(why the value of z is not increasing?)


Answers

  •   

    The value of z increases . it turns out to be 3 6 9 12. revise the concept of postfix and prefix.first at x=0 ,z assigns the value of 3 and again when loop is processed z++ calls the previous value of 3 and again adds value of y i.e 3 resulting in 6.now z has value of 6 and again it adds y(=3) becoming 9.now z is 9 and again adds 3 to it. but it wont go further because the value of z is now 12 and as z<x(=10). when z was 9 it reached the last case of the loop.



Ask Yours
Post Yours
Write your answer