Empty program
From Rosetta Code
You are encouraged to solve this task according to the task description, using any language you may 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] Agena
Actually nothing is valid code, too.
[edit] ALGOL 68
[edit] Brief form
~
[edit] BOLD form
SKIP
[edit] AmigaE
PROC main()
ENDPROC
[edit] AppleScript
An empty .scpt file is considered the smallest runnable code, but the following would also be acceptable.
return
[edit] Argile
The empty string or file are valid and do nothing.
[edit] AutoHotkey
An empty script would be enough. Adding "#Persistent" makes it persistent.
#Persistent
[edit] AutoIt
An a single comment can be considered a valid program that does nothing.
;nothing
[edit] AWK
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] Batch File
An empty batch file is syntactically correct and does nothing.
[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 ()
{
return 0;
}
[edit] C#
class Program
{
static void Main()
{
}
}
[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] Clojure
An empty file is the simplest valid Clojure program.
[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] Eiffel
Works with: SmartEiffel version 2.4
A file called main.e:
class MAIN
creation main
feature main is
do
end
end
[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
If you want to deploy a stand-alone application, that doesn't suffice though. Here's another version.
IN: rosetta.empty
: main ( -- ) ;
MAIN: main
[edit] Falcon
>
Prints an empty line.
>>
Prints nothing.
[edit] FALSE
[edit] Forth
For a Forth script to be used from a shell, you usually want the last command to be BYE in order to exit the interpreter when finished.
bye
[edit] Fortran
end
[edit] Gema
An empty program will copy input stream to output stream unchanged.
[edit] Genyris
[edit] Go
package main
func main() { }
[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] HicEst
END ! looks better, but is not really needed
[edit] Icon and Unicon
[edit] Icon
procedure main() # a null file will compile but generate a run-time error for missing main
end
[edit] Unicon
This Icon solution works in Unicon.
[edit] IDL
end
[edit] Io
[edit] J
It returns itself:
'' -: ". ''
1
[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] JavaScript
The empty file is a valid program.
[edit] Joy
#!/usr/local/bin/joy.exe
quit.
The RCE(P)L of JOY reads a program -- which looks like a sentence, ending in a full stop -- compiles it, executes it and optionally prints the top of the stack. The only way to exit this loop properly is to call quit, which is equivalent to exit(0); in C.
[edit] Lisp
Most Lisp dialects, including Common Lisp, will accept no text (no forms) as a valid program.
[edit] Logo
or end a standalone script with "bye"
#! /usr/local/bin/logo
bye
[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] Lua
[edit] M4
[edit] Mathematica
[edit] MATLAB
function [varargout] = emptyprogram(varargin)
[edit] MAXScript
An empty MAXScript file returns "OK" on execution
[edit] Metafont
end
[edit] MMIX
LOC #100
Main TRAP 0,Halt,0 // main (argc, argv) {}
[edit] Modula-3
MODULE Main;
BEGIN
END Main.
[edit] MUMPS
The empty file is a valid program.
[edit] NewLISP
;
[edit] Nimrod
[edit] Objeck
bundle Default {
class Empty {
function : Main(args : String[]) ~ Nil {
}
}
[edit] Objective-C
Works with: gcc version 4.0.1
int main(int argc, const char **argv) {
return 0;
}
The minimal empty Cocoa/OpenStep application, useful as life-support for many examples given at RosettaCode, is
#import <Cocoa/Cocoa.h>
int main( int argc, const char *argv[] )
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
NSApp = [NSApplication sharedApplication];
[pool release];
return 0;
}
[edit] OCaml
Works with: Ocaml version 3.09
;;
Actually, the smallest possible correct program in OCaml is an empty source file.
[edit] Octave
An empty text file can be a valid empty program, but since Octave has the concept of "function file" (a file containing a single function; the file is automatically loaded when a function with the same name of the file, save for the extension, is called, and the first function present in the file is used), the name of the empty file matters. E.g. calling an empty file as isempty.m makes unusable the builtin isempty function.
[edit] Oz
[edit] REPL
The simplest program that can be evaluated in the REPL (Emacs mode) is the empty program.
[edit] Accepted by compiler
The simplest 'program' that can be compiled is a file which contains a single expression.
unit
Such a 'program' cannot be executed, though.
[edit] Standalone
The simplest standalone program is a root functor that does not define anything. ("Functors" are first-class modules.)
functor
define
skip
end
[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] Perl 6
In theory, an empty file is a valid Perl 6 program that does nothing. However, Synopsis 1 decrees "The perl interpreter will assume that it is being fed Perl 5 code unless the code starts with a 'class' or 'module' keyword, or you specifically tell it you're running Perl 6 code in some other way". So, a strictly specification-compliant implementation would interpret an empty file as Perl 5. Probably the shortest file that's unconditionally a Perl 6 program is
use v6;
since it'll cause pre-6 perls to die with an appropriate error message.
[edit] PHP
An empty text file is a correct PHP program that does nothing.
[edit] PicoLisp
(de foo ())
[edit] Pike
int main(){
return 0;
}
[edit] PL/I
s: proc options (main);
end;
[edit] PL/SQL
BEGIN NULL; END;
[edit] Pop11
Pop11 has two compilers, incremental and batch compiler. For the incremental compiler one can use just empty program text (empty file), or a file containing nothing but a comment, e.g.
;;; This is a valid Pop11 program that does absolutely nothing.
The 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 the 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
An empty file is a valid PostScript program that does nothing.
Following good programming practice, however, and to ensure that a PostScript printer will interpret a file correctly, one should make the first 4 characters of the file 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] PowerShell
An empty text file is a correct Powershell script that does nothing.
[edit] PureBasic
An empty file is a correct PureBasic program that does nothing.
[edit] Python
An empty text file is a correct Python program that does nothing.
[edit] R
An empty text file is a valid empty program
[edit] Raven
An empty text file is an empty program.
[edit] REBOL
The header section is mandatory if you want it to be recognized as a REBOL program. It doesn't have to be filled in though:
rebol []
[edit] REXX
An empty file is a valid empty program; however, for some REXX implementation the first two characters must be /* (i.e. the first line must be a comment) in order to recognize the file as a REXX script:
/* empty */
[edit] Rhope
Works with: Rhope version alpha 1
Main(0,0) |: :|
[edit] Ruby
An empty file is a valid Ruby program. However, in order to make it runnable on *nix systems, a shebang line is necessary:
#!/usr/bin/ruby
[edit] Scala
object emptyProgram {
def main(args: Array[String]) {}
}
[edit] Scheme
[edit] Slate
[edit] SNUSP
$#
$ sets the instruction pointer (going right), and # halts the program (empty stack).
[edit] SNOBOL4
A valid program requires an end label. The shortest (virtually empty) program is then:
end
[edit] Standard ML
;
Actually, the smallest possible correct program in Standard ML is an empty source file.
[edit] Suneido
function () { }
[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] TI-83 BASIC
:
[edit] TI-89 BASIC
Prgm EndPrgm
[edit] Trith
[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] VBScript
An empty .vbs file is considered the smallest runnable code, but the following (a single apostrophe as comment marker) would also be acceptable (along with other non-executing instructions like option explicit.)
'
[edit] Visual Basic .NET
Works with: Visual Basic .NET version 2005
Module General
Sub Main()
End Sub
End Module
[edit] XQuery
.
The dot selects the current context node and returns it unchanged.
[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.
Since XSLT is XML, andtransformis a synonym for
stylesheet, the example above can be minified to:
<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"/>
This stylesheet echoes the text content of an XML file. The shortest stylesheet without any output would be
<transform xmlns="http://www.w3.org/1999/XSL/Transform" version="1.0"> <template match="/" /> </transform>
[edit] xTalk
Works with: HyperCard
on startup end startup
[edit] Markup Languages
[edit] HTML
<HTML></HTML>
[edit] plainTeX
\bye
[edit] LaTeX
\documentclass{article}
\begin{document}
\end{document}

