tags:

views:

625

answers:

9

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.

+5  A: 
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.

Luther Blissett
this gives NZEC error because we are required to return 0 and use int main,try it yourself.
Samuel
WTF is NZEC? The problem statement on the site does NOT say anything about `return 0` or `int main()`. This is perfectly valid C.
Chinmay Kanchi
@Luther It looks like the contest uses ANSI C, where the return in main() cannot be omitted.
schot
@schot is right.
Samuel
In the current version of C, you are allowed to omit any `return` statement in the body of `main`, you are not allowed to omit the return type (`int`) in the function declaration.
Charles Bailey
@Chinmay: I believe it stands for Non-Zero Error Code and is one of the requirements for all problems.
Roger Pate
A: 

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...

Chinmay Kanchi
This may have been true before 1999, but now C requires an explicit `int` for a return type.
Charles Bailey
with -ansi one run gave 8 as return value... so return can't be considered implicit for ansi anyway, while missing int is fine it seems
ShinTakezou
+5  A: 

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;}'.

schot
I have no idea how guys there have solved it using only 24 characters or did they cheat :P ?
Samuel
what does -DC option do? I am not allowed to change compiler flags though.
Samuel
-D gives the preprocessor a `#define` statement from the command line - so he's defining `C` to be `int main...`, and then the file itself is a single `C` which is replaced with the program from the command line.
Steven Schlansker
@Samuel: `-DC` defines a preprocessor constant, just like `#define`. So the solution above is just moving the entire program into the command line. :)
casablanca
Code golf scores would generally have to count excess characters of command line arguments against a solution.
Novelocrat
A: 

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.

R..
nice steal, mate. given the huge amount of results grepping Success in /lib and /usr/lib gives, there may just be another way.
mvds
though, perror prints on stderr. spoj wants it on stdout and they want an exit code of 0
nos
Hmm, we could `dup2(0,2);` but then the sane `puts` approach would be shorter...
R..
On GNU systems: `main(){return printf("%m\n"),0;}`.
R..
+1  A: 
int main(){perror(0);}
Winston Ewert
It says “Undefined error: 0” on Mac OS X.
KennyTM
Gcc 4.3:: warning: return type defaults to ‘int’: In function ‘main’:: warning: implicit declaration of function ‘perror’: warning: control reaches end of non-void functionrbo
rubber boots
@rubber boots: warnings are irrelevant in code golf.
Michael Foukarakis
+1  A: 

30 characters:

main(){exit(!printf("%m\n"));}

29 characters:

main(){exit(!printf("%m
"));}
R..
A: 

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

rubber boots
That isn't truly zero, since you're writing "x" to the source file, before compiling. Besides in the problem in question, I think the programmer has no control over the compilation flags.
Pedro Rodrigues
Pedro, you are right. I edited the answer after your remark. How is this a "Code Golf" if such approaches are not wanted ;-)
rubber boots
+1  A: 

26 characters

main(){brk(printf("%m"));}
Michael Foukarakis
WTF is the use of `brk` here? Just a random bogus way to put 0 in `%eax`?
R..
Pretty much, yeah.
Michael Foukarakis
+3  A: 

26

main(){j0(!printf("%m"));}
ehpc
Neat. Does it compile without -lm?
Michael Foukarakis
Yep, but with warnings. spoj accepts it though.
ehpc
Probably works just as well if you remove the `!`...
R..
Nope it doesn't. Bessel function of positive number is not zero. So spoj won't accept it.
ehpc
Close. Hint for the 24 long solution: http://en.wikipedia.org/wiki/Exit_status#Unix
ypsu