1

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;
 }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
huahua sun
  • 11
  • 1
  • 2
    Do you know that the postfix increment/decrement operators return the *old* value (before modification)? – UnholySheep Mar 18 '22 at 16:13
  • You probably meant `--x` instead of `x--`. Read closely the parts dealing with post-/prefix operators in your learning material. – Jabberwocky Mar 18 '22 at 16:19
  • 1
    "--x" is an example of an [prefix operation](https://www.geeksforgeeks.org/difference-between-increment-and-decrement-operator). The decrement will occur FIRST, hence you should see the decremented value in the "printf". "x++", on the other hand, is a "postfix" operation. Which is the reason you don't see it. – paulsm4 Mar 18 '22 at 16:25

3 Answers3

0

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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.

hygull
  • 8,464
  • 2
  • 43
  • 52