Find the output for the following code

rengaraj

rengaraj

@rengaraj-89Rrct Oct 7, 2024

Sir / madam,
My question is

Find the output for the following code

void main()
{
int i=7;
printf("%d",i++*i++);
}

Advance Thanks,
R.Rengaraj
{Java Certification Quiz}
{On this forum my 20th Question.}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 11, 2009

    Is the answer 56?

  • rengaraj

    rengaraj

    @rengaraj-89Rrct Dec 11, 2009

    hi SarveshGupta,
    Your answer is right, but i am confused with the answer. Can you explain me the process ?
    R.Rengaraj

  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 11, 2009

    i++ means using post increment that is when we use it in any expression it uses the current value of that variable that is being carried forward from previous statement and then increment after compiler reads it so that any next time compiler reads the variable again it will read the incremented value

    now we are doing i++ * i++ this implies

    first time the value read is 7 which was the initial value of i and it gets incremented so for next operand i++ it takes the value 8 and after using 8 it increments it to 9

    since we are using them only till here we get the value as 7*8 =56
    if we try to print the value of i at the end of the program it will be 9

    void main()
    {
    int i=7;
    printf("%d \n",i++*i++);
    printf("%d",i);
    }

    will give the output as:

    56
    9
  • gaurav.bhorkar

    gaurav.bhorkar

    @gauravbhorkar-Pf9kZD Dec 11, 2009

    @sarvesh
    You explained it perfectly, buddy.

  • rengaraj

    rengaraj

    @rengaraj-89Rrct Dec 11, 2009

    sarveshguptai++ means using post increment that is when we use it in any expression it uses the current value of that variable that is being carried forward from previous statement and then increment after compiler reads it so that any next time compiler reads the variable again it will read the incremented value

    now we are doing i++ * i++ this implies

    first time the value read is 7 which was the initial value of i and it gets incremented so for next operand i++ it takes the value 8 and after using 8 it increments it to 9

    since we are using them only till here we get the value as 7*8 =56
    if we try to print the value of i at the end of the program it will be 9

    void main()
    {
    int i=7;
    printf("%d \n",i++*i++);
    printf("%d",i);
    }
    will give the output as:

    Dear sarveshgupta,
    Your answer is right, but you stated as 7*8 why i can be 8*7,
    bcoz, if execution is being handled from right to left.
    R.Rengaraj

  • gaurav.bhorkar

    gaurav.bhorkar

    @gauravbhorkar-Pf9kZD Dec 12, 2009

    rengarajDear sarveshgupta,
    Your answer is right, but you stated as 7*8 why i can be 8*7,
    bcoz, if execution is being handled from right to left.
    R.Rengaraj

    Good Point. In fact printf statement handles execution from right to left (in C).
    In this case it won't affect the answer in either case.

  • Sahithi Pallavi

    Sahithi Pallavi

    @sahithi-oJZaYj Dec 12, 2009

    @sarvesh : Very good explanation.!

    @rengaraj : Nice point..!

  • rengaraj

    rengaraj

    @rengaraj-89Rrct Dec 12, 2009

    Sir,
    Thanks to all of you. For your excellent co-operation.
    R.Rengaraj

  • sarveshgupta

    sarveshgupta

    @sarveshgupta-txtmu5 Dec 12, 2009

    Ya I am sorry for writing it that way but i actually meant 8 * 7 as I know printf reads from right to left

    I wrote it simply missing the order of writing

  • rengaraj

    rengaraj

    @rengaraj-89Rrct Dec 12, 2009

    No, Problem Sarveshgupta