Obscure C code (was Re: Excessive optimization)

Tony Duell ard at p850ug1.demon.co.uk
Fri Dec 3 13:29:40 CST 2010


> 
> Fred Cisin wrote:
>  > 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"]);
> }

I am not a C programmer, or indeed any sort of programmer, but my answers 
are : 

It is valid C code

It should do the same as the BASIC program
10 PRINT "Hello, woeld"
20 END
-- that is it will send the string 'Hello, world' followed by a newline to 
stnadard out

Fromw what I rememebr A[i] in C is just a shorthand for indirecting from a 
pointer, and i[A] should be exactly the same thing. Hence the program 
thakss one character at a time from the character array containing 
"Hello, world\n" and output it.

-tony



More information about the cctech mailing list