So I was losing my week-end on StackOverflow and saw this challenge in the Hot Network Questions.
Background
Hello golfers! I would like to learn all the programming languages! But I kinda have a short attention span... and copying all the Hello World examples gets boring... but I like fire! ^w^
Challenge
So here is the plan! I want you all to write the smallest code that will compile, print Goodbye Cruel World!, and then crash. Or, as a bonus twist challenge, print Hello World! and crash with Goodbye Cruel World!
As a student willing to understand wholly the C language, I have been very confused when stumbling upon the C answer to this challenge :
main(){puts(puts("Goodbye Cruel World!"));}
Prints the string and then tries to use the return value as a pointer to another string to be printed, which causes a segmentation fault.
Thanks to puts() documentation I have found that puts() returns a non-negative value on success. So if I understood correctly, this is equivalent to something like :
puts(2);
How 2 is "a pointer to another string to be printed" ??
Later, an improvement has been added to this very same answer :
main(i){i=puts("Goodbye Cruel World!")/0;}
And this time I am totally lost. So i is taken as an argument from main, used to store the return value of puts(). Ok. But what about the \0 ? Why use the NUL-TERMINATOR character there ?
If you could please lighten me up a bit, it would be very interesting for me to understand this. Also, I think the title of the question could be a bit more accurate if rephrased but I haven't been able to put in words my misunderstand.
And to answer your second question :
main(i){i=puts("Goodbye Cruel World!")/0;}
There is a difference between '\0' and /0
The first one is the NUL character but the second one is a division by zero. So this code tries to divide the result of puts by zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With