Live data from Hacker News

C puzzles

gowrikumar.com

71–80 of 97 posts

Re: C puzzles

#71
post #35

The last question: > Write a C program which prints Hello World! without using a semicolon!!! That seems daunting. At first.

This could be fun to golf... smallest I've got is main(){if(puts("Hello World!")){}}

  main(a,b){if(puts(*(int*)b)){}}
Needs to be compiled as a 32-bit program, and the executable renamed to Hello\ World!

:)

Re: C puzzles

#72

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…

Most of the modern IDE's have built-in auto code formatters. You can style your code according to your preference with just a couple of clicks. Unless you are using the plain old NOTEPAD for coding.

Re: C puzzles

#73
post #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, a…

I wouldn't mind working with your coding style either: though I don't have the same standards for braces and array initialization, you apply your style consistently across the whole code.

Re: C puzzles

#74
I have a problem with these puzzles that it seems like no one has pointed out. A couple of them are just intentionally misleading, specifically the ones that involve typos. For example, the solution to one of them is that it says 'defa1ut' instead of 'default' in a case statement. Ignoring the question of whether or not typos are really "puzzles", this is misleading because the syntax highlighting the author uses highlights it as if it had been spelled correctly. No actual (automatic) syntax highlighting system would ever highlight the code as it is shown on the page. It should either be presented with correct highlighting or with none at all.

For anyone who is going through these exercises, I would encourage you to copy-and-paste the code snippets into a regular text editor.

Re: C puzzles

#75
1. Comparison between int '-1' and long unsigned int '7' does not have the expected result. this is because

"Binary operations between different integral types are performed within a "common" type defined by so called usual arithmetic conversions (see the language specification, 6.3.1.8). In your case the "common" type is unsigned int. This means that int operand (your b) will get converted to unsigned int before the comparison, as well as for the purpose of performing subtraction." [1]

Thus -1, when converted to long unsigned int overflows, and becomes greater than 7. The loop exits immediately.

_______

2. error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘-’ token void OS_HP-UX_print() ^

a '-' in the function name is illegal

_______

3. continue triggers the while(false) condition, exits the loop.

_______

4. the stdout output is buffered. Adding a newline after the fprintf statement does the trick. see [2]

_______

5. In C macros the octothorpe turns what follows it into a string. It does so without expanding the expression. Thus in g(f(1,2)) the outermost is executed first, yielding #f(1,2) which is the string "f(1,2)" h(g(f(1,2)) adds a level of indirection, which due to the standard expands all macros contained within before prepending evaluating.

"After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded. Before being substituted, each argument’s preprocessing tokens are completely macro replaced as if they formed the rest of the preprocessing file; no other preprocessing tokens are available"

see [3]

_______

6. Typo "defau1t" -> "default"

_______

7. https://news.ycombinator.com/item?id=12921707

_______

[1] http://stackoverflow.com/questions/2084949/arithmetic-operat...

[2] http://stackoverflow.com/questions/14784367/cs-printf-and-fp...

[3] http://stackoverflow.com/questions/3323231/argument-preceded...

Re: C puzzles

#76

I have a problem with these puzzles that it seems like no one has pointed out. A couple of them are just intentionally misleading, specifically the ones that involve typos. For example, the solution to one of them is that it says 'defa1ut' instead of 'default' in a case statement. Ignoring the question of whether or not typos are really "puzzles", this is misleading because the syntax highlighting the author uses hig…

Oh yes! That's what is mentioned in the web page upfront. "Most of the programs are meant to be compiled, run and to be explained for their behaviour. The puzzles/questions can be broadly put into the following categories: "

Re: C puzzles

#78
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…

Yup! that's correct. Later versions of compilers have become intelligent with standard library function and I doubt if this reproes on the current compilers - for standard library functions.

Re: C puzzles

#79

I have a problem with these puzzles that it seems like no one has pointed out. A couple of them are just intentionally misleading, specifically the ones that involve typos. For example, the solution to one of them is that it says 'defa1ut' instead of 'default' in a case statement. Ignoring the question of whether or not typos are really "puzzles", this is misleading because the syntax highlighting the author uses hig…

> No actual (automatic) syntax highlighting system would ever highlight the code as it is shown on the page.

Not true: my actual automatic syntax highlighting system (Vim) uses the same colour for reserved words and for labels. So I think it's fair play.

Re: C puzzles

#80
post #33
post #28

Earlier quoted context omitted.

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

No. The int to int * truncation does not happen, the cast does not matter. What matters is the `int *p' which is 64-bit on 64-bit systems, so the compiler will just move the returned value in RAX into wherever p is, so no truncation happens, even without the cast. Look at the generated assembly, you'll see what I mean.

VC++ pops a CDQE in there. For ``p=(int *)malloc(1000);'', with no prototype for malloc, it generates this:

        mov     ecx, 1000
        call    malloc
        cdqe
        mov     QWORD PTR p$[rsp], rax
I'll have to check if it's still true today, but Windows/the VC++ CRT certainly used not have no qualms about handing out pointers to memory below the 4GByte mark. So if you have a problem like this, it can go undetected for quite some time...

(Don't know about Linux. 64-bit OS X binaries usually start with a 4GByte section at 0, so the bottom 4GBytes simply isn't available.)

Post reply on HN