I really dislike his choice of coding style. Compare: int CountBits (unsigned int x ) { static unsigned int mask[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF } ; int i ; int shift ; /* Number of positions to shift to right*/ for ( i =0, shift =1; i > shift) & mask[i]); return x; } as opposed to: int countBits (unsigned int x) { static unsigned int mask[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x0…
C puzzles
11–20 of 97 posts
Re: C puzzles
#12I really dislike his choice of coding style. Compare: int CountBits (unsigned int x ) { static unsigned int mask[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF } ; int i ; int shift ; /* Number of positions to shift to right*/ for ( i =0, shift =1; i > shift) & mask[i]); return x; } as opposed to: int countBits (unsigned int x) { static unsigned int mask[] = { 0x55555555, 0x33333333, 0x0F0F0F0F, 0x0…
Re: C puzzles
#13Re: C puzzles
#14That IA-64 one is really puzzling, anyone know what the heck is happening?
This is why it's considered bad form to cast the result of malloc in C. Of course, modern compilers will warn about implicit prototypes, and as of C99 it's no longer legal.
Re: C puzzles
#15I'd have called it "gotchas" or "puzzlers" instead of "puzzles". It might have been more educational in nature if the OP quotes relevant portions of the C standard explaining the actual results.
#include int main() { int a=10; switch(a) { case '1': printf("ONE\n"); break; case '2': printf("TWO\n"); break; defa1ut: printf("NONE\n"); } return 0; } If you expect the output of the above program to be NONE, I would request you to check it out!!
Re: C puzzles
#16Earlier quoted context omitted.
I was about to say I didn't think it made a big difference but I have no idea what's going on with that intraline whitespace.
To me, having consistent indentation does make a substantial difference - it is fundamental to understand at a glance what structures are present in the code, rather than "parsing" it in detail every time I'm looking for something.
Having more compact code allows to have more code on the screen without using the scrollbar. For me, it is also more readable because I am used to it.
Re: C puzzles
#17I'd have called it "gotchas" or "puzzlers" instead of "puzzles". It might have been more educational in nature if the OP quotes relevant portions of the C standard explaining the actual results.
This one, for example. It's just a typo, not really a "puzzle." (apparently I don't know how to write code on hn) #include int main() { int a=10; switch(a) { case '1': printf("ONE\n"); break; case '2': printf("TWO\n"); break; defa1ut: printf("NONE\n"); } return 0; } If you expect the output of the above program to be NONE, I would request you to check it out!!
Edit: two, not four. Thanks, jonsen.
Re: C puzzles
#18That IA-64 one is really puzzling, anyone know what the heck is happening?
Re: C puzzles
#19That IA-64 one is really puzzling, anyone know what the heck is happening?
Re: C puzzles
#20I'd have called it "gotchas" or "puzzlers" instead of "puzzles". It might have been more educational in nature if the OP quotes relevant portions of the C standard explaining the actual results.
This one, for example. It's just a typo, not really a "puzzle." (apparently I don't know how to write code on hn) #include int main() { int a=10; switch(a) { case '1': printf("ONE\n"); break; case '2': printf("TWO\n"); break; defa1ut: printf("NONE\n"); } return 0; } If you expect the output of the above program to be NONE, I would request you to check it out!!