EmbLogic's Blog

Solve the Mystery of pre and post increment operators

Assume any value of k eg 1

Q1 d=k++/++k……..print d and k, k=3;

why is the reult 1 for d???

Q2 d=printf(“%d%d%d”,++k,k,k++);

 

printf(“%d%d%d”,k++,k,k++);

Why the result vary in both the printf statements.???

Q3 printf(“%d%d%d”,k,k++,++k);

what will be the output and WHY???

Q4 printf(“%d%d%d”,++k,k++,k);

output and WHY??

Q5 printf(“%d%d%d”,k++,++k,k);

output and WHY??

5 Responses to Solve the Mystery of pre and post increment operators

  1. sahil sharma says:

    before i give u solution,i will tell you how a post and preincrement work.
    1.the operation is start from right to left and printing the value from left to right.
    2.In preincrement first the value is increment then operation is performed and In postincrement first operation is performed then increment.
    3.In postincrement the value is print after the operation is performed but in preincrement the value is print after all the operation is performed and print value from left to right.
    qns1.assume the value of k is 1.
    d=k++/++k;
    it means first value of k is 2 through ++k and now the value of k is 2,so d=2/2=1.
    other problem u can solve easily through this rule…

  2. arjumand says:

    hahaha……….its not possible to get the right answer at one go

Leave a Reply to sahil sharma Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>