Talk:Increment and decrement operators

Undefined behavior

edit

Is it worth noting that using pre- and post- increment in the same expression results in undefined behavior in many languages? Thomblake (talk) 16:19, 16 June 2011 (UTC)Reply

I added a paragraph about undefined behavior, which should probably cover that specific point. — Loadmaster (talk) 18:34, 16 June 2011 (UTC)Reply

Incorrect definition

edit

The article states that the "increment operator increases the value of its operand by 1", but this is only sometimes correct, and misses the point of the operator. The operator actually increases the value by an amount that reflects the number of machine words required to store the data. So if it's a character pointer on a byte-oriented machine, it increments by 1. If it's a pointer to a 4-byte int, then it increments by 4. If it's a pointer to a float on a machine that represents floating-point numbers in a single word, then it increments by 1, but on a modern byte-oriented machine it might increment it by 8. The point is, the compiler knows the size of the items and the machine's word-alignment requirements, and increments by the correct amount so the sense of the operation is always "step to the next thing". If the operand is an integer, then ++ steps to the next integer value, which is the same as adding 1, but this is only one narrow use case (which happens to be the one many people know about and use often). The article, as written, ignores the major value the operator has for programs that use pointers and structs. — Preceding unsigned comment added by 199.27.168.23 (talk) 02:43, 22 February 2018 (UTC)Reply

I agree. "Step to the next thing" is the primary meaning of increment. I added a few words about this -- incrementing a pointer to a 106 byte structure increases the pointer by 106, etc.
I see that others also seem confused by "incrementing" often increasing the value of a pointer by some amount other than 1 byte -- "C struct pointer iteration"; "How to use a pointer with an array of struct?", etc.
How can we make this less confusing? --DavidCary (talk) 22:55, 5 August 2021 (UTC)Reply

Sample code

edit

In the sample C code, the indices (i and n) should be of type unsigned rather than int — Preceding unsigned comment added by 67.183.37.183 (talk) 15:48, 28 February 2020 (UTC)Reply

I'd rather they stay as int, since that's a basic type and the decrement operator can be used to allow them to go negative. +mt 21:14, 1 March 2020 (UTC)Reply

Does this really need to be a separate article from this?

edit
212.75.236.148 (talk) 18:45, 22 July 2023 (UTC)Reply