curly brace position

Dear colleagues, I have found a strange bit of code that works, but I think it should not.

#include <stdio.h>
int main ()
{
int iC = 0;
for (iC = 1; iC < 5; iC++) {
switch (iC)
case 1:
{ // Wrong?, but it works fine!.
printf(“ONE\n”);
break;
case 2:
printf(“TWO\n”);
break;
default:
printf(“NO!\n”);
break;
}
}
return 0;
}

The code compiles without any warning using clang 3.4.1 (FreeBSD 10.1) and “-Wall” “–pedantic”. And the execution results are:
ONE
TWO
NO!
NO!

Of course, I have tried the code with the curly brace in its right position:
switch (iC) {
case 1:
And the results are the same.

I have compared the assembly files (.s) and both are the same, disregarding the curly brace position.

Could anybody explain why both codes work the same way?

I have tried with “Microsoft Visual C 6” and “Microsoft Visual Studio 2010” with the same results. Also GCC compiles it without any warning (sorry for the version).

Best Regards,

Germán Marcos.

You might want to google “Duff’s Device”

— Marshall

Dear colleagues, I have found a strange bit of code that works, but I think it should not.

#include <stdio.h>
int main ()
{
int iC = 0;
for (iC = 1; iC < 5; iC++) {
switch (iC)
case 1:
{ // Wrong?, but it works fine!.
printf(“ONE\n”);
break;
case 2:
printf(“TWO\n”);
break;
default:
printf(“NO!\n”);
break;
}
}
return 0;
}

The code compiles without any warning using clang 3.4.1 (FreeBSD 10.1) and “-Wall” “–pedantic”. And the execution results are:
ONE
TWO
NO!
NO!

You might want to google “Duff’s Device”

— Marshall

Thanks a lot!