On 12/2/10 5:03 PM, Eric Smith wrote:
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"]);
}
So far none of the self-styled C experts I've interviewed were able to
answer all three questions correctly.
Note that I do not claim to be a C expert; I am just skeptical of most
people who claim to be such. I don't expect a C expert to necessarily
know the answer offhand, but they should be able to figure it out. If
they can't, they clearly don't know C anywhere near well enough to be
considered expert, as this problem is based on fundamental principles
of the language.
Eric
It prints "Hello, world!" and a newline (no quotes).
It works because C compilers treat array subscripts as simple pointer
arithmetic, so i["He...\n"] is the same as (i * sizeof(char)) +
"He...\n" (the string is a converted to a pointer internally).