C golfing tips - returning 0 without return statement

5

I'm looking for some solution in returning values in main function, I need to shorten my program more, return 0 takes too much characters of my code, I need to return 0, or my code fails. I know that in C there can be no return statement, and then program will use result of last operation - but on which operations I can 'really' shorten my code? At the moment I found brk(0) as shortest return.

main(n){while(~scanf("%i",&n))printf("%i ",n*~n/-2);brk(0);}

encoree1337

Posted 2015-02-01T08:55:33.617

Reputation: 51

2Which code-golfing contest requires that main return 0? – feersum – 2015-02-01T09:49:50.587

It would help to know some things: Can n be 0? Does extra whitespace invalidate the answer? – feersum – 2015-02-01T10:06:09.423

1@feersum it's from spoj challenges, code has to pass test - it has to return 0 or it does not pass it because of error, n can be <1,1001>, it can print answers with spaces, or new lines, it does not matter, judge checks numbers, i can even write 100x ' ' between each result – encoree1337 – 2015-02-01T11:55:52.057

1

@encoree1337 "in C there can be no return statement, and then program will use result of last operation" - I think you are pushing it a bit there. Can you point to that being specified anywhere? As opposed to the result of last operation just coincidentally being left in the accumulator, which is where the return value would be on x86 (but not necessarily on other architectures)? http://en.wikipedia.org/wiki/X86_calling_conventions

– Mawg says reinstate Monica – 2015-03-11T12:40:59.917

Answers

6

You don't need to do anything.

This assumption:

then program will use result of last operation

is erroneous. If there is no return statement, main returns 0. It is not dependent on whatever might be left over in registers or anything like that.

From the standard:

5.1.2.2.3 Program termination

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

(emphasis mine)

Your program's main already returns zero, or something is wrong somewhere else.

Leushenko

Posted 2015-02-01T08:55:33.617

Reputation: 1 500

2Perhaps it should be noted that the standard you are referring to is C99, not C89 (which is the default on most compilers), in which omitting the return 0; resulted in undefined behaviour.

So yes, C99 and C++ allow you to omit the return 0; thing. – Stefano Sanfilippo – 2015-03-10T22:54:00.463

(and consequently, C11 too) – Stefano Sanfilippo – 2015-03-10T22:59:15.950

1I believe the server in question indeed uses gcc in C89 mode, so this auto-return rule does not apply. – feersum – 2015-03-11T09:15:12.120

2

To be precise, the return value is generally dependent not on any previous "operation", but on the return value of the last function call. You are hoping that the function dumps a 0 into the EAX register, and that it stays there when main returns.

You may be able to get better mileage out of atoi/atol and gets here. When gets hits EOF without reading a line, it returns a null pointer. Then, atol will return 0 upon receiving a null pointer as input. Since 0 is not an allowed value for n, this can be used as the loop condition, as well as the way to drop 0 into the appropriate register.

main(n){while(n=atol(gets(??????)))printf("%i ",n*~n/-2);}

The only problem is to find some memory for gets to write on. I'm not familiar with the Unix-specific library functions such as brk, but surely you can find something 6 characters or less (needed for an overall shortening) which returns char* or void* among all the cruft.

feersum

Posted 2015-02-01T08:55:33.617

Reputation: 29 566