[Solved] Does printf alter variables?


The output will be 1.

Your expression ++x will be

x = x+1;

In both the printf() you get 1

So the value of x is modified with the pre-increment operator here and in printf() in the second line prints the new value of x which is 1

printf() just prints the value of x it doesn’t modify your variable

Note the difference between pre-increment and post-increment .

With post-increment you will get 0 in the first printf() because the value of x is fetched by the printf()'s %d and later x gets incremented leading to the value of x in the second prinf() to be 1

solved Does printf alter variables?