16 ms·
Bjarne should have called it ++C.
by jpcfl 2y ago
Bjarne should have called it ++C.
- card_zero 2y agoBecause people choose to use pre-increment by default instead of post-increment? Why is that?
- jejdjdbd 2y agoWhy would you use post increment by default? The semantics are very particular. Only on very rare occasions I need post increment semantics. And in those cases I prefer to use a temporary to make the intent more clear
- card_zero 2y agoPeople seem to mostly write a typical for loop ending with ; ++i){ But I write ; i++){ and seeing it the other way round throws me off for a minute, because I think, as you put it, why would you use those very particular semantics? But I guess this is only a semantic argument.
- johannes1234321 2y ago> why would you use those very particular semantics? The difference is that i++ has to keep a copy to the original around as the return value is the pre-increment value, while with ++i that isn't needed as the resulting value is being returned. In the for loop that shouldn't matter as a) for an integer it is essentially for free (it is just reordering when the relevant register is set) and b) that value is hopefully optimized out anyways by the compiler, however as there are cases where it matters some people prefer the ++i style, some just think it looks better.
- layer8 2y agoIn C++ the semantics can differ, in that copying an object for post-increment might require a memory allocation internally (for example in the case of a BigInt class), which may fail and throw an exception. For consistency, using pre-increment by default and unless you really need post-increment, is a good habit.
- Quekid5 2y agoI've seen either style, but it the argument about which is proper is pointless. Any modern compiler will optimize either equally well, unless you're doing something that actually depends on the order of the increment.
- kanbankaren 2y agoNo, for fundamental datatypes pre/post-increment doesn't matter, but for classes that overload those operators, the postfix form creates a temporary object hence people write for(auto it = begin(v); it != end(v); ++it)
- secondcoming 2y agoIt makes no difference if the increment is done on an int, but it can make a different if your `i` is some object with its own ++ operator.
- codr7 2y agoIf you're used to the idiom, the intent couldn't be clearer. I miss it when switching between C/++ and other languages.
- flohofwoe 2y agoI rarely use pre-increment tbh, but post-increment all the time for array indices (since typically the array should be indexed with the value before the increment happens). If the pre- or post-increment behaviour isn't actually needed, I prefer `x += 1` though.
- tialaramex 2y agoWhy use this operator? Like most C and C++ features the main reason tends to be showing off, you learned a thing (in this case that there are four extra operators here) and so you show off by using it even if it doesn't make the software easier to understand. This is not one of those beginner -> journeyman -> expert cycles where coincidentally the way you wrote it as a beginner is identical to how an expert writes it but for a very different reason. I'd expect experts are very comfortable writing either { x = k; k += 1; } or { k += 1; x = k; } depending on which they meant and don't feel an itch to re-write these as { x = k++; } and { x = ++k; } respectively. I'm slightly surprised none of the joke languages add equally frivolous operators. a%% to set a to the remainder after dividing a by 10, or b** to set b as two to the power b or some other silliness.
- cozzyd 2y agoIt's more useful for pointers than for values, IMO
- layer8 2y agoThe idiomatic void strcpy(char *s, char *t) { while (*s++ = *t++) ; } (straight from K&R) wouldn’t work without it.
- n_plus_1_acc 2y agoWhich many people find unreadable compared to other versions.
- beeforpork 2y agoAnd for several reasons. * is it (*s)++ or *(s++)? * it is not *++s nor ++*s And I have seen *(*s)++ in some places! It is concise syntax but very confusing.
- chongli 2y agoK&R actually teaches this as a desirable idiom? People should not be recommending K&R to beginners today!
- int_19h 2y agoIt should be ++C because with C++ the value you get from the expression is the old one. If you're asking why people use pre-increment by default instead of post-increment, it's mostly historical. The early C compilers on resource-constrained platforms such as early DOS were not good at optimization; on those, pre-increment would be reliably translated to a simple ADD or INC, whereas code for post-increment might generate an extra copy even if it wasn't actually used. For C++ this was even worse with iterators, because now it depended on the compiler's ability to inline its implementation of postfix ++, and then prove that all the copies produced by that implementation have no side effects to optimize it to the same degree as prefix ++ could. Depending on the type of the underlying value, this may not even be possible in general. The other reason is that all other unary operators in C are prefix rather than postfix, and mixing unary prefix with unary postfix in a single expression produces code that is easy to misunderstand. E.g. *p++ is *(p++), not (*p)++, even though the latter feels more natural, reading it left-to-right as usual. OTOH *++p vs ++*p is unambiguous.
- card_zero 2y agoK&R seems to use pre-increment early on, then post-increment consistently (or a lot, anyway, I haven't done a thorough check) after chapter 3, in situations where either would do. In fact, after introducing post-increment at 2.8.
- jpcfl 2y ago> It should be ++C because with C++ the value you get from the expression is the old one. You get it!
- wpollock 2y agoThe PDP-11 that C originally targeted had address modes to support the stack. Pre-increment and post-decrement therefore did not require a separate instruction; they were free. After the PDP-11 went the way of the dodo, both forms took a machine cycle so it (mostly) became a stylistic issue. (The two operators have different semantics, but the trend to avoid side-effects in expressions means that both are most often used in a single expression statement like "++x;" or "x++;", so it comes down to your preferred style.)
- zabzonk 2y agoPlease explain what you mean by "a separate instruction".
- spc476 2y agoSome idiomatic C code to copy a string (I'm not saying this is good C code, but it's just an example): while(*d++ = *s++) ; On the Motorola 68000 (based somewhat on the PDP-11) the code would look like: loop: move.b (a0)+,d0 move.b d0,(a1)+ bne loop while on the x86 line, it would be: loop: mov al,[rsi] mov [rdi],al inc rsi ; extra instruction! inc rdi ; extra instruction! cmp al,0 jne loop Yes, there are better ways to write that code for both the 68K and x86, but I hope this gets the point across.
- wang_li 2y ago> loop: move.b (a0)+,d0 move.b d0,(a1)+ ... > loop: mov al,[rsi] mov [rdi],al This hurts my brain. When we invent time machines I'm going to use it to go back and slap whoever at intel came up with that operand order.
- spc476 2y agoIt hurts less if you think of it like assignment: al = [rsi] [rdi] = al
- wnoise 2y agoNah. It's just the natural semantics -- he added stuff to C, but returned something that wasn't actually more advanced...