5 ms·
I confess to just scanning the code and being cold on C right now, but isn't ++x a post increment? That is, it occurs after the rest of the expression has been
by DanielBMarkham 14y ago
I confess to just scanning the code and being cold on C right now, but isn't ++x a post increment? That is, it occurs after the rest of the expression has been evaluated? (as opposed to x++, which is a pre-increment) Just guessing that the perhaps the issue is when the actual overflow is occurring.
- deleted 14y ago[deleted]
- deleted 14y ago[deleted]
- dag11 14y ago++x evaluates to x's new (incremented) value. x++ evaluates to x's old (pre-incremented) value.
- vhf 14y agoIt is the opposite, like in many other languages. e.g. int i = 0; printf("%i %i", i++, ++i); // prints "0 2" Same goes in C, C++, Java, PHP, ... [EDIT] Turns out this is a bad example, as "the order in which function arguments are evaluated is undefined" (cf. below) Correct is : int i = 0; printf("%i", i++); // prints 0 printf("%i", ++i); // prints 2
- simias 14y agoI believe you trigger an undefined behaviour there. You modify i twice in the same statement.
- excuse-me 14y agoIs that defined behaviour? Is there any rule saying that args to a function have to be evaluated in a particular order - ie is ',' a function point?
- decklin 14y agoActually, the order in which function arguments are evaluated is undefined. http://c-faq.com/expr/comma.html http://c-faq.com/expr/comma.html You can use a comma in other expressions to introduce a sequence point: http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=expr#seqpoints http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=expr#seqpoints
- vhf 14y agoThanks for your clever comment and for those emitting similar concerns. You surely are right. I wanted to give a quick example, turns out it was a bad one. Next time I'll write : int i = 0; printf("%i", i++); // prints 0 printf("%i", ++i); // prints 2
- hythloday 14y agoturns out it was a bad one Given that you were making a point on an article about the complexity of C, I'd say it was an unintentionally excellent example.
- sirclueless 14y agoIf you really want to make your brain hurt, there was an article on HN a while ago about the following statement: i = i++; Evaluating that expression takes a real dive into the guts of the C spec.
- jim_kaiser 14y agoThe standard says, evaluation order of function arguments is undefined.. but in general, printf evaluates right to left. This was a common puzzle as I remember.
- CodeMage 14y agoIt's precisely the other way around: ++x is a pre-increment and x++ is a post-increment.
- acqq 14y agoMnemonic: read as when you would read the text, left to right: ++x increment than use the value x. x++ use the value x then increment.
- deleted 14y ago[deleted]
- loeg 14y agoNope, you've got the two backwards.