Is formatted output equivalent?

46 Views Asked by At

When I was doing the practice questions today, I found that the outputs of printf("%d\n",x--); and printf("%d\n",x); are the same.

I changed it to printf("%d\n",x++); and found to be the same. I want to know why.

#include<stdio.h>

int main()
{
   int x;
   scanf_s(" %d", &x);

   if (x++ > 5)
       printf("%d",x);
   else
       printf("%d\n",x--);

       return 0;
 }
3

There are 3 best solutions below

0
ikegami On

You used the post-decrement/-increment operators.

x-- decrements x and returns the original value of x.

x++ increments x and returns the original value of x.

To get the desired behaviour, use the pre-decrement/-increment operators.

--x decrements x and returns the modified x.

++x increments x and returns the modified x.

0
Vlad from Moscow On

According to the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it)

and

3 The postfix -- operator is analogous to the postfix ++ operator, except that the value of the operand is decremented (that is, the value 1 of the appropriate type is subtracted from it).

Consider this pair of calls of printf

int x = 10;
printf( "%d\n", x++ ); 
printf( "%d\n", x ); 

The output is

10
11

That is the value of the expression of x++ is the value of its operand that is equal to 10. As a side effect the variable x is increased and in the second call of printf the new value of x after applying the side effect is outputted.

You can imagine this call

printf( "%d\n", x++ );

like

printf( "%d\n", x ), x += 1;

Opposite to postfix increment and decrement operators the values of unary increment (++x) and decrement (--x) operators are values after incrementing and decrementing their operands.

From the C Standard (6.5.3.1 Prefix increment and decrement operators)

2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation.

and

3 The prefix -- operator is analogous to the prefix ++ operator, except that the value of the operand is decremented.

0
hygull On

That's simple and you can understand as well if you know the usage of these 2 kind of operators.

Post Increment/Decrement Operator e.g. a++, a-- etc.

So when we use it in the statement, the current value of the variable will be used and post that its value is incremented by 1.

Just take a look into my comments at end of statements (shows the o/p).

#include<stdio.h>

int main()
{
   int x;
   scanf_s(" %d", &x); // 10

   printf("%d", x++);  // 10
   printf("%d", x);    // 11
   return 0;
}

Pre Increment/Decrement Operator, e.g. ++a, --a etc.

So when we use it in the statement, first its value of variable is incremented by 1 & then the incremented value will be used.

#include<stdio.h>

int main()
{
   int x;
   scanf_s(" %d", &x);   // 10

   printf("%d", ++x);    // 11
   printf("%d", x);      // 11
   return 0;
}

I think, now it's clear to you. For info on usages, just take a look into Usage of Pre/Post Increment & Decrement operators in C.