[Klug-general] Obscure C at saturday's meeting

James Morris jwm.art.net at gmail.com
Mon Nov 29 09:41:14 UTC 2010


for (i = 0; i < 10; stuff(++i));

will pass values 1 - 10 to stuff, whereas

for (i = 0; i < 10; stuff(i++));

will pass values 0 - 9 to stuff.

++i is (prefix) increment i before evaluating i
i++ is (postfix) increment i after evaluating i

(hint, they can be used in conditions)

for (i = 0; ++i < 10; stuff(i));



On 29 November 2010 06:59, Thomas Edward Groves <teg451013 at freeuk.com> wrote:
> From Tom Groves
> At the meeting on 27th November Julia put
> forward an example of highly ambiguous C.
> {
> for (i = 0; i < 10; stuff(i++));
> more_stuff();
> }
> With the question: how many times is
> more_stuff() executed?
> (Answer: once. Note the ; at the end of the for line.)
>
> I pointed out that judicious use of indent
> could show up this kind of crap and she asked
> me to demonstrate so here it is:
> {
>  for (i = 0; i < 10; stuff (i++));
>  morestuff ();
>  for (i = 0; i < 10; stuff (i++))
>    morestuff ();
> }
> I have since thought of another reason to pound
> this damn coder's head.
> For what values is stuff() executed?
> No, not 0-9, it's actually 1-10!
>
> And that's all from your favourite(?)
> (re)tired old command-line dinosaur.
>
> Tom
>
>
>
> _______________________________________________
> Kent mailing list
> Kent at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/kent
>



-- 
_
: http://jwm-art.net/
-audio/image/text/code/



More information about the Kent mailing list