What's a C expert?

Herwig Henseler henseler at uniol.UUCP
Thu Aug 17 00:09:41 AEST 1989


> }>           What do you need to know to be an expert C programmer?
> }How about ... understands why a[i] equals i[a] and CAN EXPLAIN IT,
That's really nice, but not all compiler are willed to compile it...

Another nice example:

	int i = 0;

	i += ++i + i++;
	printf( "%d\n", i );

Now explain what can happen.....


Solution:
Every number between 1 and 5 (inclusive) is legal! Our compiler produces '5'
and I had to look at the machine-code to figure out why:

i += ++i + i++;     expands to (our compiler evaluates from left to right):

/*  a.    b.    c. */
i = ++i + i++ + i;
First i is incremented at pos. a, i equals 1 and is incremented at pos. b.
Now position b evaluates to 1 and pos a to 2 (!! strange but legal !!). At
pos. c i has the value 2. Now 2+1+2 results in 5. Voila!

(The explanation of the four other possible results are left as an exercise
for junior C-experts :-)

	bye, Herwig
--
** Herwig Henseler (CS-Student) D-2930 Varel, Tweehoernweg 69 | Brain error- **
** EMail: henseler at uniol.UUCP (..!uunet!unido!uniol!henseler) | core dumped  **



More information about the Comp.lang.c mailing list