6
\$\begingroup\$

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);}
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Which code-golfing contest requires that main return 0? \$\endgroup\$
    – feersum
    Feb 1, 2015 at 9:49
  • \$\begingroup\$ It would help to know some things: Can n be 0? Does extra whitespace invalidate the answer? \$\endgroup\$
    – feersum
    Feb 1, 2015 at 10:06
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ Feb 1, 2015 at 11:55
  • 1
    \$\begingroup\$ @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)? en.wikipedia.org/wiki/X86_calling_conventions \$\endgroup\$ Mar 11, 2015 at 12:40

2 Answers 2

8
\$\begingroup\$

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.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Perhaps 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. \$\endgroup\$ Mar 10, 2015 at 22:54
  • \$\begingroup\$ (and consequently, C11 too) \$\endgroup\$ Mar 10, 2015 at 22:59
  • 3
    \$\begingroup\$ I believe the server in question indeed uses gcc in C89 mode, so this auto-return rule does not apply. \$\endgroup\$
    – feersum
    Mar 11, 2015 at 9:15
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.