Live data from Hacker News

C puzzles

gowrikumar.com

11–20 of 97 posts

Re: C puzzles

#11

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…

[deleted]

Re: C puzzles

#12

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…

I prefer the one who takes a dedicated line for the `{`. It leaves me the ability to group together chunks of related functionality, whereas the latter method throws out that option instantly.

Re: C puzzles

#14

That IA-64 one is really puzzling, anyone know what the heck is happening?

There's no #include first, so you get an implicit prototype for malloc. With an implicit prototype, the function is assumed to return int. The cast then converts the returned int to int*. This works on 32-bit where int is the size of a pointer, but on 64-bit with 32-bit ints, the top half of the pointer gets chopped off and you end up with a nonsense value.

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

#15
post #8

I'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!!

Re: C puzzles

#16

Earlier 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.

Consistency is the main point. The second layout is more compact and this makes a huge difference if you need to put code in a document (also a small difference for websites).

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

#17
post #8

I'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!!

Place two or more spaces at the beginning of a line to get code formatting.

Edit: two, not four. Thanks, jonsen.

Re: C puzzles

#20
post #8

I'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!!

It is just a typo. What makes it "puzzling", though, is that it compiles.
Post reply on HN