Shortest way to print "Success"
I have been trying this problem SUCCESS at spoj but I am not able to get optimal solution to that problem
I tried
int main(){return !puts("Success");}
but it takes 45 characters. Any alternate suggestions to solve the problem? People have solved it using 24 characters also.
main(){puts("Success");}
24 characters.
- in C, if you omit the return type, it is implicitly
int
- if main() does not contain a return statement, then the return value of main is 0
UPDATE: All right, the return from main can be omitted in every version of C, but only C99 defines the return value to be 0 if omitted. C99 also disallows implicit declarations.
UPDATE: I have faint memory that somebody pulled this off for a similar problem: He/She somehow encoded most of the program in the file name so that the __FILE__ macro could be used in the program code to inject the code. I don't know if this is within the rules for OP's competition, but it should make an interesting exercise anyway.
The problem statement is very vague, it look like it needs to compile with gcc -ansi
and return 0 when run. Best I could come up was this:
main(){exit(!puts("Success"));}
32 characters counting the final newline (can you omit that?). Adding int
to main()
puts it at 36 characters.
Edit
This probably won't be allowed:
/* Filename: Success */
main(){exit(!puts(__FILE__));}
Compile with gcc -x c -ansi Success
and it will save you another character!
And what about this one character solution:
C
Just compile with gcc -ansi -DC='int main(void){puts("Success");return 0;}'
.
26 characters
main(){brk(printf("%m"));}
26
main(){j0(!printf("%m"));}
int main(){perror(0);}
30 characters:
main(){exit(!printf("%m\n"));}
29 characters:
main(){exit(!printf("%m
"));}
Here's a simple one in 24 chars...
main(){puts("Success");}
Neither the int
return type for main()
nor the return
statement are strictly necessary...
Making it smaller relies on doing things which are not portable or strictly valid C. On a POSIX environment with lax enforcement of the rules, you can do (18 characters):
main(){perror(0);}
But unless int
and pointers have the same size, this will break. Adding 1 extra character and taking advantage of the fact that real-world POSIX systems have long
and pointer types the same size:
main(){perror(0L);}
I don't think you can make it any smaller.
I made it in 0 (zero) characters:
cat src.c
(empty)
the resulting executable:
rbo@deneb ~
$ ./src
SUCCESS
and the command line:
rbo@deneb ~
$ echo x>>src.c && gcc -Dx="int main(){return puts(\"SUCCESS\");}" -o src src.c
But this of course still creates a source file with on character, as Pedro has pointed out below. Without any source file, the command would be (in a Unix environment):
rbo@deneb ~
echo 'int main(){return puts("SUCCESS");}'|gcc -ansi -o src -xc -
which is even shorter than the above. In the original description, there's no restriction stated how to solve it, but the OP adds in a later comment he can't change the command line. If this is true, then this is not really a Code Golf ;-)
Regards
rbo
精彩评论