-3
#include <stdlib.h>
#include <stdio.h>

int main()
{
    int i = 0;
    i = 10; printf("++i, ++i :--> The value is: %d, %d\n", ++i, ++i);
    i = 10; printf("++i, i++ :--> The value is: %d, %d\n", ++i, i++);
    i = 10; printf("++i, --i :--> The value is: %d, %d\n", ++i, --i);
    i = 10; printf("++i, i-- :--> The value is: %d, %d\n", ++i, i--);
    i = 10; printf("++i, i   :--> The value is: %d, %d\n", ++i, i);
    i = 10; printf("i++, ++i :--> The value is: %d, %d\n", i++, ++i);
    i = 10; printf("i++, i++ :--> The value is: %d, %d\n", i++, i++);
    i = 10; printf("i++, --i :--> The value is: %d, %d\n", i++, --i);
    i = 10; printf("i++, i-- :--> The value is: %d, %d\n", i++, i--);
    i = 10; printf("i++, i   :--> The value is: %d, %d\n", i++, i);

    return 0;
}

Output:

++i, ++i :--> The value is: 12, 12
++i, i++ :--> The value is: 12, 10
++i, --i :--> The value is: 10, 10
++i, i-- :--> The value is: 10, 10
++i, i   :--> The value is: 11, 11
i++, ++i :--> The value is: 11, 12
i++, i++ :--> The value is: 11, 10
i++, --i :--> The value is: 9, 10
i++, i-- :--> The value is: 9, 10
i++, i   :--> The value is: 10, 11

Would you please let me know how the compiler is doing the execution.

stackoverflow
  • 2,320
  • 3
  • 17
  • 21
Subhajit
  • 87
  • 4
  • 2
    The compiler is free to order the evaluation of function arguments in any way it wishes to. – Vicky May 01 '13 at 10:58
  • 1
    The behavior is undefined. See http://stackoverflow.com/questions/3812850/output-of-multiple-post-and-pre-increments-in-one-statement – gatinueta May 01 '13 at 10:59
  • In practice though it's usually the order they're pushed onto the stack for a varargs function which is right-to-left. Though that doesn't seem to match your results? But it is what I see in Visual Studio 2012. What are you using? – Rup May 01 '13 at 11:00
  • @Rup On 64-bit linux, the first arguments to `printf` are passed in registers, the stack is only used when there are too many. – Daniel Fischer May 01 '13 at 11:17
  • Just a note: clang produces different output. – Daniel Fischer May 01 '13 at 11:22
  • 3
    @Rup: in practice ... it’s still undefined. Assuming that you can predict what this code will do is a big mistake unless you know what compiler is being used and you wrote the relevant section of the compiler in question. – Stephen Canon May 01 '13 at 11:23
  • @Stephen Sure, I'd never rely on that behaviour, but that doesn't mean you can't look at the assembly to explain what the results you're getting as an academic exercise. And on 32-bit x86 systems that's the only way I've ever seen it work. – Rup May 01 '13 at 11:28
  • 1
    @Rup: the compiler is free to place the function arguments on the stack in whatever order it deems most efficient (i.e. it needn’t sequentially `push` them), and it doesn’t need to *evaluate* them in the same order as it puts them on the stack. There exist compilers that will produce different results, even on x86. – Stephen Canon May 01 '13 at 11:56

1 Answers1

-2

In i++

  • That perform Operation like Arithmetic Operation Or Any Other Code On i Value
  • After That It Increments 1 Or Add 1 value To The i.

i=10; printf("i: %d" , i++)

Answer is: 10.

In above the compiler first print the answer and after add 1 value...

In ++i

  • That first add 1 value to the i
  • and after perform another operation on i.
Kalpesh Rajai
  • 2,040
  • 27
  • 39