Live data from Hacker News

C puzzles

gowrikumar.com

21–30 of 97 posts

Re: C puzzles

#21
post #17

Earlier quoted context omitted.

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.

  Text after a blank line 
  that is indented by two 
  or more spaces is reproduced verbatim.
  (This is intended for code.)

Re: C puzzles

#22
post #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.

Huh??? Can you please elaborate on that? Because that makes no sense to me at all.

Re: C puzzles

#23

Earlier quoted context omitted.

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 .

  It is just a typo.
The compiler considers it to be a label.

Perfectly legitimate.

Re: C puzzles

#24
post #23

Earlier quoted context omitted.

It is just a typo. What makes it "puzzling", though, is that it compiles .

It is just a typo. The compiler considers it to be a label . Perfectly legitimate.

Yep. Not particularly baffling in the context of a puzzle, but I could definitely see that as being a pretty fun typo to debug.

Although I'd think the syntax highlighting would typically be a little less misleading...

Re: C puzzles

#25
post #10

I found this presentation pretty useful http://www.slideshare.net/olvemaudal/deep-c

Alternate link: http://www.pvv.org/~oma/DeepC_slides_oct2011.pdf

The girl character is very pedantic and probably not a team player, I'll just hire the guy.

Re: C puzzles

#26

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

Has nothing to do with includes, it has no error. The C compiler will automatically know its imported. He probably thinks that `int *` is 32-bit regardless.

Re: C puzzles

#27
post #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 m…

There is no cast that happens behind the scene, look at the generated assembly.

Re: C puzzles

#28
post #27
post #14

Earlier quoted context omitted.

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 m…

There is no cast that happens behind the scene, look at the generated assembly.

Do you mean that the cast in the code gets compiled out?

Re: C puzzles

#29

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 agree with you on some counts, but not on others.

  unsigned int countBits (unsigned int x)
  {
      int i;
      int shift; // number of positions to shift to the right
      static unsigned int mask[] = {
      0x55555555,
      0x33333333,
      0x0F0F0F0F,
      0x00FF00FF,
      0x0000FFFF};

      for (i = 0, shift = 1; i > shift) & mask[i]);

      return x;
  }
Where to put the first '{' depends on your development environment, as some hide the line where the bracket is, and some don't (when you hide a function or loop). Array initialization format is a tricky one, as the spacing is very dependent on how you are trying to visualize the data, but I would agree that all (leftmost) elements should be aligned to the same margin. I find the blank line between array and other declarations puzzling for both examples. I prefer to leave blank lines between code of the same indent (outside of declarations and initializations). Commenting using '//' allows you to comment out blocks of code on both sides of your line comment, so I'm with you there.

P.S. I do not like Hacker News' paragraph formatting

Re: C puzzles

#30

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…

The purpose of these puzzles is to move you out of your comfort zone. Take the none familiar indentation as part of that experience.
Post reply on HN