Find the output for the following code

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

  • sarveshgupta
    sarveshgupta
    Is the answer 56?
  • rengaraj
    rengaraj
    hi SarveshGupta,
    Your answer is right, but i am confused with the answer. Can you explain me the process ?
    R.Rengaraj
  • sarveshgupta
    sarveshgupta
    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
    @sarvesh
    You explained it perfectly, buddy.
  • rengaraj
    rengaraj
    sarveshgupta
    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:
    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
    rengaraj
    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
    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
    @sarvesh : Very good explanation.!

    @rengaraj : Nice point..!
  • rengaraj
    rengaraj
    Sir,
    Thanks to all of you. For your excellent co-operation.
    R.Rengaraj
  • sarveshgupta
    sarveshgupta
    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
    No, Problem Sarveshgupta

You are reading an archived discussion.

Related Posts

Firefox mainly survives because of the different Add-ons / Extensions and now Google Chrome supporting this would mean that the market share is going to boost in the year 2010....
Hello CEs, it's my first post, this web site is very interesting, i knew that i should introduce my self, so let's start: My Name: Hussein Barakat i'm in the...
Harpreet Singh Bedi (Ranbir Kapoor) just about scrapes through college with no clue of what he is going to do next. Unable to do an MBA due to poor scores...
MediaMonkey Version: 3.2.0.1294 File Size: 7621 License: Free Operating System: Windows XP,Windows Vista,Windows 7 If you've got a gargantuan media collection in dire need of reorganization, MediaMonkey is a must....
Industry grew by a robust 10.3 per cent in October against a paltry 0.1 per cent a year ago, powered by manufacturing, particularly consumer durables, which was driven by the...