-4

Why is the decrement operator -- not bringing the value down by 1 when executed?

int a = 20;
int c ;

c = a--;

Inspecting the value of c now, it should be 19, yet it comes out as 20. What am I missing?

jscs
  • 63,694
  • 13
  • 151
  • 195
  • You might want to print `a` as well: `c` has the value of `a` before decrementing. Decrementing affects only `a`. If you want `c` to be one less than `a` without changing `a`, just use `c = a - 1`. – M Oehm Dec 30 '14 at 12:12

4 Answers4

1

a-- is Post-Decrement, what you need --a Pre-Decrement. Please read Increment and decrement operators on Wiki

The following C code fragment illustrates the difference between the pre and post increment and decrement operators:

int  x;
int  y;

// Increment operators
x = 1;
y = ++x;    
// x is now 2, y is also 2
y = x++;
// x is now 3, y is 2

// Decrement operators
x = 3;
y = x--;    
// x is now 2, y is 3
y = --x;    
// x is now 1, y is also 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
0

what you're using is called a postfix operator. It will get executed [decrement the value] after the assignment = operator has finished its execution with the existing value.

To be clear, in case of post decrement, the ..-- operator is evaluated and the decrement is scheduled once the other evaluations including that operand are finished. It means, the existing value of the operand is used in the other evaluation [in =] and then the value is decreased.

If you want, try printing the value of a itself. It will print the decremented value.


EDIT:

If my choice of words in my answer created any confusions, for the reference, from the c99 standard, chapter 6.5.2.4, [emphasis mine]

[For increment] The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented [......]

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).

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Is there any practical use for a postfix? when would it be beneficial for the calculation to be done after the program has been evaluated... – StevieMack Dec 30 '14 at 12:26
  • @StevieMack yes, obviously !!! Think of a counter in `loop`ing conditions. – Sourav Ghosh Dec 30 '14 at 12:28
  • @StevieMack also, think of this `*destination++ = *source++`. maybe you can roll your own string copy function with this. :-) – Sourav Ghosh Dec 30 '14 at 12:30
  • @StevieMack - `Is there any practical use for a postfix?` - Its just a style. Normally one would not want to write `i=i-1` for pre-increment or type an extra line for post like `some_statement; i=i-1`. – Sadique Dec 30 '14 at 12:30
  • WOW !!! and the reason for downvote, please? [ @ dear downvoter] – Sourav Ghosh Dec 30 '14 at 12:31
  • `after the assignment = operator has finished its execution.` As per the standard `In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations (§1.9/15) The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. `3 – Sadique Dec 30 '14 at 12:32
  • @StevieMack Please go through http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points - the above answer is not infallible. – Sadique Dec 30 '14 at 12:35
  • @al-Acme I'm not too sure what you're trying to say. I never said something about _one_ expression getting evaluated more than once. What i mentioned is which value will be used while performing the decrement operation. – Sourav Ghosh Dec 30 '14 at 12:38
  • I now see that this question has answers, however, I simply did not have the language necessary to find this. If a person does not know the word for something it makes it difficult. – StevieMack Dec 30 '14 at 21:41
  • @SouravGhosh Do't care about those downVotes :D Everything will become good at some time! – Rizier123 Jan 28 '15 at 20:53
  • @Rizier123 Thanks. Hoping the same. Cheers Mate !! – Sourav Ghosh Jan 29 '15 at 09:54
0

you should use --a (pre decrement operator), you are using post decrement operator a--

The result of the postfix -- operator is the value of the operand. As a side effect, the value of the operand object is decremented (that is, the value 1 of the appropriate type is subtracted to it).

µtex
  • 900
  • 5
  • 12
0

You are using the post decrement. Post decrement means first use the value in a variable or anything then decrement the value in the variable. So in this case first value of a will assigned to c. And the decrement is done. You can check printing the value of a.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31