SPOJ problem: Life, the Universe, and Everything
The working solution is quite good, actually. You check the return value of scanf(), and you use the while loop condition smartly to say exactly what you mean to accomplish.  The only thing wrong with it is the indentation.

Here is one reasonable way to format it.

#include &lt;stdio.h&gt;

int main(void)
{
    int i = 0;
    while (scanf("%d\n", &amp;i) &gt; 0 &amp;&amp; i != 42) 
    {
        printf("%d\n", i);
    }
    return 0;
}


The initialization of i to 0 and return 0 are actually not needed, but I don't have a problem with making them explicit.

As for your failed attempts, they fail because they attempt to store all of their input, which isn't necessary to do. When there are more than 9 inputs, the buffer overflows.