CrazyEngineers
  • Find the output for the following code

    rengaraj

    rengaraj

    @rengaraj-89Rrct
    Updated: Oct 7, 2024
    Views: 1.1K
    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.}
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sarveshgupta

    MemberDec 11, 2009

    Is the answer 56?
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 11, 2009

    hi SarveshGupta,
    Your answer is right, but i am confused with the answer. Can you explain me the process ?
    R.Rengaraj
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 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
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberDec 11, 2009

    @sarvesh
    You explained it perfectly, buddy.
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 11, 2009

    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
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberDec 12, 2009

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberDec 12, 2009

    @sarvesh : Very good explanation.!

    @rengaraj : Nice point..!
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 12, 2009

    Sir,
    Thanks to all of you. For your excellent co-operation.
    R.Rengaraj
    Are you sure? This action cannot be undone.
    Cancel
  • sarveshgupta

    MemberDec 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
    Are you sure? This action cannot be undone.
    Cancel
  • rengaraj

    MemberDec 12, 2009

    No, Problem Sarveshgupta
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register