Obscure C code (was Re: Excessive optimization)

Cameron Kaiser spectre at floodgap.com
Thu Dec 2 17:07:40 CST 2010


> > Yes, C DOES permit an empty condition clause. Valid, and often used when
> > a programmer prefers to use the "for" rather than "while" keyword.
> 
> C permits a lot of interesting things.  I've occasionally interviewed 
> programmers claiming to be C experts, and amongst my questions for them 
> are whether the following is valid C code, what will happen when it is 
> executed, and why:
> 
> #include <stdio.h>
> int main ()
> {
>    int i;
>    for (i = 0; i < 14; i++)
>      putchar (i ["Hello, world!\n"]);
> }
> 
> So far none of the self-styled C experts I've interviewed were able to 
> answer all three questions correctly.

Okay, I compiled this and was shocked at the result (I didn't think it
would work, honestly). So what are the answers to your questions above?
I'm genuinely interested.

>
-- 
------------------------------------ personal: http://www.cameronkaiser.com/ --
  Cameron Kaiser * Floodgap Systems * www.floodgap.com * ckaiser at floodgap.com
-- Haiku is easy/Sometimes it doesn't make sense/Refrigerator -- "KFI Haikus" -



More information about the cctalk mailing list