Empty Program
From Rosetta Code
Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.
In this task, the goal is to create the simplest possible program that is still considered "correct."
[edit] Programming Languages
[edit] Ada
Works with: GCC version 4.1.2
procedure Empty is begin null; end;
[edit] ALGOL 68
[edit] Brief form
~
[edit] BOLD form
SKIP
[edit] AppleScript
An empty .scpt file is considered the smallest runnable code, but the following would also be acceptable.
return
[edit] AWK
Works with: AWK
Works with: gawk
Works with: awka
Works with: awkcc
The empty string (or file) is recognised as valid program that does nothing.
The program
1
is the simplest useful program: like the UNIX command 'cat', it prints every line of the files given as arguments, or (if no arguments are given) the standard input.
[edit] BASIC
Works with: QBasic
10 END
[edit] Befunge
@
The halt command @ is required because code wraps around. An empty file would be an infinite loop.
[edit] Brainf***
Empty program
Note: this works as all non-instruction characters are considered comments. Alternatively, a zero-byte file also works.
[edit] C
Works with: gcc version 4.0.3
int main (int argc, char **argv) { // Note: The arguments may be omitted return 0; }
[edit] C#
Works with: Visual C# version 2005
static class Program { static void Main(string[] args) { } }
[edit] C++
Works with: g++ version 4.0.3
int main ( int /*argc*/, char * * /*argv*/ ) { // Unused arguments should not be named // There are variations: // 1: main ''may'' explicitly return a value // (other non-void-returning C++ functions ''must'' do so, // but there's a special exception for main that falling off it // without an explicit return is equivalent to a "return 0;" at // the end of the main function) // 2: The arguments may be omitted entirely }
[edit] Clean
module Empty Start world = world
Compile the project with No Console or No Return Type to suppress printing of the value of the world.
[edit] D
Works with: DMD
Works with: GDC
void main() {}
[edit] Delphi
- See Pascal
[edit] E
The shortest possible program:
This is equivalent to:
null
[edit] eC
or
class EmptyApp : Application
{
void Main()
{
}
}
[edit] Erlang
An empty module:
module(empty).
An empty Erlang script file (escript):
main(_) -> 1.
[edit] eSQL
CREATE COMPUTE MODULE ESQL_Compute CREATE FUNCTION Main() RETURNS BOOLEAN BEGIN RETURN TRUE; END; END MODULE;
[edit] Factor
[edit] Forth
: main ;
[edit] Fortran
end
[edit] Groovy
[edit] Haskell
Standard: Haskell 98
The simplest possible program is a single module using the implicit module header "module Main(main) where", and defining the action main to do nothing:
main = return ()
The simplest possible module other than Main is one which contains no definitions:
module X where {}
[edit] haXe
class Program {
static function main() {
}
}
Unlike most languages haXe doesn't have arguments in the main function because it targets different platforms (some which don't support program arguments, eg: Flash or Javascript). You need to use the specific libraries of the platform you are targeting to get those.
[edit] IDL
end
[edit] Io
[edit] Java
Works with: Java version 1.5+
public class EmptyApplet extends java.applet.Applet { @Override public void init() { } }
public class EmptyMainClass { public static void main(String... args) { } }
The "..." basically means "as many of these as the programmer wants." Java will put multiple arguments into an array with the given name. This will work for any method where an array is an argument, but with a twist. A call can be made like this:
method(arg0, arg1, arg2, arg3)
All of the args will be put into an array in the order they were in the call.
Works with: Java version 1.0+
public class EmptyMainClass { public static void main(String[] args) { } }
public class EmptyApplet extends java.applet.Applet { public void init() { } }
@Override - Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message. It's present from JDK 5.0 (1.5.0) and up.
Actually this is not strictly correct. The smallest possible correct program in Java is an empty source file.
[edit] Lisp
This is common to all LISP implementations, such as Common Lisp.
()
This also prints its own source upon execution.
[edit] LSE64
As with Forth, an empty file is the shortest program. To exit the interpreter at the end of a loaded file:
bye
[edit] MAXScript
An empty MAXScript file returns "OK" on execution
[edit] Objective-C
Works with: gcc version 4.0.1
int main(int argc, const char **argv) { return 0; }
[edit] OCaml
Works with: Ocaml version 3.09
;;
Actually, the smallest possible correct program in OCaml is an empty source file.
[edit] Pascal
program ProgramName; begin end.
The first line is not necessary in modern Pascal dialects. With today's most compilers, the empty program is just:
begin end.
[edit] Perl
Works with: Perl version 5.8.8
#!/usr/bin/perl 1;
#!/usr/bin/perl exit;
#!/usr/bin/perl # A program without a body will work too
#!/usr/bin/perl
The smallest possible program is an empty file (zero length). This requires you to specify the interpreter instead of relying on the shell's shebang magic, thus: perl empty.pl.
The smallest possible Perl one-liner is perl -e0.
[edit] PHP
<?php ?>
[edit] PL/SQL
BEGIN NULL; END;
[edit] Pop11
Pop11 has two compilers, incremental and batch compiler. For incremental compiler one can use just empty program text (empty file). Batch compiler generates an executable which starts at a given entry point, so one should provide an empty function. If one wants program that works the same both with incremental compiler and batch compiler the following may be useful:
compile_mode :pop11 +strict; define entry_point(); enddefine;
#_TERMIN_IF DEF POPC_COMPILING entry_point();
Here batch compiler will stop reading source before call to entry_point while incremental compiler will execute the call, ensuring that in both cases execution will start from the function entry_point.
[edit] PostScript
In general, the first 4 characters of the file have to be
%!PS
If a particular version of the PS interpreter is needed, this would be included right there:
%!PS-2.0 % ...or... %!PS-3.0 % etc
[edit] Python
Works with: Python version 2.4.3
An empty text file is an empty program:
[edit] Raven
An empty text file is an empty program.
[edit] Rhope
Works with: Rhope version alpha 1
Main(0,0) |: :|
[edit] Ruby
[edit] Scheme
()
In the same way as with Lisp, this is also the shortest Scheme program that prints its own source upon execution.
[edit] SNUSP
$#
$ sets the instruction pointer (going right), and # halts the program (empty stack).
[edit] Tcl
Nothing is mandatory in Tcl, so an empty file named nothing.tcl would be a valid "empty program".
[edit] Toka
For interpreted code, nothing is required, although bye is necessary for an empty script to exit (rather than wait for the user to exit the listener). Hence:
bye
Or, for a directly runnable script:
#! /usr/bin/toka bye
For compiled code, the simplest program is an empty quote:
[ ]
Again, to exit the listener, you will still need user input if this is not followed with bye.
[edit] UNIX Shell
Works with: Bourne Shell
#!/bin/sh
Works with: Bourne Again SHell
#!/bin/bash
[edit] Unlambda
i
(See how i plays so many roles in unlambda?)
[edit] Visual Basic .NET
Works with: Visual Basic .NET version 2005
Module General
Sub Main()
End Sub
End Module
[edit] XSLT
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> </xsl:stylesheet>
Add other namespaces to the stylesheet attributes (like xmlns:fo="http://www.w3.org/1999/XSL/Format") if you use them.
[edit] xTalk
Works with: HyperCard
on startup end startup
[edit] Markup Languages
[edit] LaTeX
Works with: pdfeTeXk version 3.141592-1.30.4-2.2 (Web2C 7.5.5)
Works with: LaTeX2e version 2003/12/01
\documentclass{article}
\begin{document}
\end{document}
Categories: Programming Tasks | Basic language learning | Ada | ALGOL 68 | AppleScript | AWK | BASIC | Befunge | Brainf*** | C | C sharp | C++ | Clean | D | Delphi | E | EC | Erlang | ESQL | Factor | Forth | Fortran | Groovy | Haskell | HaXe | IDL | Io | Java | Lisp | Common Lisp | LSE64 | MAXScript | Objective-C | OCaml | Pascal | Perl | PHP | PL/SQL | Pop11 | PostScript | Python | Raven | Rhope | Ruby | Scheme | SNUSP | Tcl | Toka | UNIX Shell | Unlambda | Visual Basic .NET | XSLT | XTalk | LaTeX

