Obscure C code (was Re: Excessive optimization)

Fred Cisin cisin at xenosoft.com
Thu Dec 2 18:48:39 CST 2010

On Thu, 2 Dec 2010, Eric Smith wrote:
> > A[i++] = i++;
> I can't quote chapter and verse, but I'm fairly sure the standard will
> say that the result of that assignment is undefined.

C can be pretty loose about letting you do such.

> Many programmers would say "I would never write code like that", but
> I've seen statements like that creep into code as a result of
> cut-and-paste or global-search-and-replace operations that weren't
> carefully checked.  It's also quite possible when writing complex
> expressions to come up with things like that which are not necessarily
> obvious to casual inspection.  It is good that many compilers will
> detect these things and generate warnings.  When I have the power to do
> so, I generally treat warnings from the compiler as fatal errors
> requiring a fix, and not acceptable in released code.

absolutely.  At the bare minimum, understand what is being pointed out.
It's hard, but necessary, to get beginning students to realize that an
assignment is not complete just because the output has the right number.

#define SIX 1+5
#define NINE 8+1
printf("What you get when you multiply SIX by NINE: %d", (SIX * NINE) );

"The mice will be furious."

Grumpy Ol' Fred     		cisin at xenosoft.com

More information about the cctech mailing list