From Wikipedia, the free encyclopedia.
A "hello world" program is a computer program that simply prints out "Hello, world!". It is used in many introductory tutorials for teaching a programming language and many students use it as their first programming experience in a language. A "hello world" program is typically one of the simpler programs possible in a computer language. Some are surprisingly complex, especially in some Graphical User Interface (GUI) contexts. Some others are surprisingly simple, especially those which heavily rely on a particular shell to perform the actual output.
A "hello world" program can be a useful sanity test to make sure that a language's compiler, development environment, and run-time environment are correctly installed. Configuring a complete programming tool chain from scratch to the point where even trivial programs can be compiled and run may involve substantial amounts of work. For this reason, a simple program is used first when testing a new tool chain.
While small test programs existed since the development of programmable computers, the tradition of using the phrase "Hello, world!" as the test message was influenced by an example program in the book The C Programming Language, by Brian Kernighan and Dennis Ritchie. The example program from that book prints "hello, world" (i.e., no capital letters, no exclamation sign).
A collection of "hello world" programs written in various computer languages can serve as a simple "Rosetta Stone" to assist in learning and comparing the languages.
Here are some examples in different languages:
msg$: db 'Hello, world!$'
end start
DATASEG
MSG DB 'Hello, world!$'
CODESEG
MOV AX, @data
MOV DS, AX
MOV DX, OFFSET MSG
MOV AH, 09H ; DOS: output ASCII$ string
INT 21H
MOV AX, 4C00H
INT 21H
END
HELLO: MOV #MSG,R1
MOVB (R1),R0
LOOP: .TTYOUT
MOVB +(R1),R0
BNE LOOP
.EXIT
MSG: .ASCIZ /HELLO, WORLD!/
.END HELLO
.entry start,0
; establish a channel for terminal I/O
$assign_s devnam=term_name,-
chan=term_chan
blbc r0,error
; queue the I/O request
$qio_s chan=term_chan,-
func=#io$_writevblk,-
iosb=out_iosb,-
p1=msg,-
p2=#13
blbc r0,error
$exit_s ; normal exit
error: halt ; error condition
.end start
.program
LET START () BE
$(
WRITES ("Hello, world!*N")
$)
LIBRARY 'SYS$LIBRARY:STARLET';
EXTERNAL ROUTINE
LIB$PUT_OUTPUT;
GLOBAL ROUTINE HELLO_WORLD =
BEGIN
LIB$PUT_OUTPUT(%ASCID %STRING('Hello World!'))
END;
END
ELUDOM
int main(void) {
printf("Hello, world!\\n");
return 0;
}
int main() {
std::cout << "Hello, world!\\n";
}
Start :: String
Start = "Hello, world"
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY "Hello, world!".
STOP RUN.
creation
make
feature
make is
local
io:BASIC_IO
do
!!io
io.put_string("%N Hello, world!")
end -- make
end -- class HELLO_WORLD
hello_world() -> io:fwrite("Hello, world!\\n").
includes Framework;
HAS PROPERTY IsLibrary = FALSE;
forward Hello;
-- START CLASS DEFINITIONS
class Hello inherits from Framework.Object
has public method Init;
has property
-- START METHOD DEFINITIONS
------------------------------------------------------------
method Hello.Init
begin
super.Init();
task.Part.LogMgr.PutLine('HelloWorld!');
end method;
-- END METHOD DEFINITIONS
HAS PROPERTY
main = putStr "Hello World\\n"
(startup
(strategy MEA)
(make request ^action hello)
)
class HelloWorld : public Gtk::Window {
public:
HelloWorld();
virtual ~HelloWorld();
protected:
Gtk::Button m_button;
virtual void on_button_clicked();
};
HelloWorld::HelloWorld()
: m_button("Hello, world!") {
set_border_width(10);
m_button.signal_clicked().connect(SigC::slot(*this,
&HelloWorld::on_button_clicked));
add(m_button);
m_button.show();
}
HelloWorld::~HelloWorld() {}
void HelloWorld::on_button_clicked() {
cout << "Hello, world!" << endl;
}
int main (int argc, char *argv[]) {
Gtk::Main kit(argc, argv);
HelloWorld helloworld;
Gtk::Main::run(helloworld);
return 0;
}
public class HelloFrame extends Frame {
HelloFrame(String title) {
super(title);
}
public void paint(Graphics g) {
super.paint(g);
java.awt.Insets ins = this.getInsets();
g.drawString("Hello, world!", ins.left + 25, ins.top + 25);
}
public static void main(String args [])
{
HelloFrame fr = new HelloFrame("Hello");
fr.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit( 0 );
}
}
);
fr.setResizable(true);
fr.setSize(500, 100);
fr.setVisible(true);
}
}
class HelloWorld : public QWidget
{
Q_OBJECT
public:
HelloWorld();
virtual ~HelloWorld();
public slots:
void handleButtonClicked();
QPushButton *mPushButton;
};
HelloWorld::HelloWorld() :
QWidget(),
mPushButton(new QPushButton("Hello, World!", this))
{
connect(mPushButton, SIGNAL(clicked()), this, SLOT(handleButtonClicked()));
}
HelloWorld::~HelloWorld() {}
void HelloWorld::handleButtonClicked()
{
std::cout << "Hello, World!" << std::endl;
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
HelloWorld helloWorld;
app.setMainWidget(&helloWorld);
helloWorld.show();
return app.exec();
}
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
char szClassName[] = "MainWnd";
HINSTANCE hInstance;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wincl;
hInstance = hInst;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.style = 0;
wincl.hInstance = hInstance;
wincl.lpszClassName = szClassName;
wincl.lpszMenuName = NULL; //No menu
wincl.lpfnWndProc = WindowProcedure;
wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); //Color of the window
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION); //EXE icon
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); //Small program icon
wincl.hCursor = LoadCursor(NULL, IDC_ARROW); //Cursor
if (!RegisterClassEx(&wincl))
return 0;
hwnd = CreateWindowEx(0, //No extended window styles
szClassName, //Class name
"", //Window caption
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, //Let Windows decide the left and top positions of the window
120, 50, //Width and height of the window,
NULL, NULL, hInstance, NULL);
//Make the window visible on the screen
ShowWindow(hwnd, nCmdShow);
//Run the message loop
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 15, 3, "Hello, world!", 13);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
HelloWorld Program says:
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, world!", 100, 50);
}
}
The following sequence of characters, expressed in hexadecimal notation (with carriage return and newline characters at end of sequence):
Line-oriented (aka Console)
ABC
WRITE "Hello World"
Ada
with Ada.Text_Io; use Ada.Text_Io;
procedure Hello is
begin
Put_Line ("Hello, world!");
end Hello;AmigaE
PROC main()
WriteF('Hello, World!')
ENDPROCAPL
'Hello World'
Assembly language
Accumulator-only architecture: DEC PDP-8, PAL-III assembler
See the example section of the PDP-8 article.First successful µP/OS combinations: Intel 8080/Zilog Z80, CP/M, RMAC assembler
bdos equ 0005H ; BDOS entry point
start: mvi c,9 ; BDOS function: output string
lxi d,msg$ ; address of msg
call bdos
ret ; return to CCPAccumulator + index register machine: MOS 6502, CBM KERNAL, ca65 assembler
MSG: .ASCIIZ "Hello, world!"
LDX #0
LDA MSG,X ; load initial char
@LP: JSR $FFD2 ; chrout
INX
LDA MSG,X
BNE @LP
RTSAccumulator/Index microcoded machine: Data General Nova, RDOS
See the example section of the Nova article.
Expanded accumulator machine: Intel x86, MS-DOS, TASM
MODEL SMALL
IDEAL
STACK 100H
General-purpose-register CISC: DEC PDP-11, RT-11, MACRO-11
.MCALL .REGDEF,.TTYOUT,.EXIT
.REGDEFCISC on advanced multiprocessing OS: VAX, VMS, MACRO32
.title hello
term_name: .ascid /SYS$INPUT/
term_chan: .blkw 1
out_iosb: .blkq 1
msg: .asciz /Hello, world!/
RISC machine: ARM, RISC OS, BBC BASIC Assembler
ADR R0,message
SWI "OS_Write0"
SWI "OS_Exit"
.message
DCS "Hello, world!"
DCB 0
ALIGNAWK
BEGIN { print "Hello, world!" }BASIC
MS BASIC (traditional, unstructured)
10 PRINT "Hello, world!"
20 END
TI-BASIC
:Disp "Hello, world!"
StarOffice/OpenOffice Basic
sub main
print "Hello, World"
end subStructured BASIC
print "Hello, world!"
BCPL
GET "LIBHDR"
BLISS
%TITLE 'HELLO_WORLD'
MODULE HELLO_WORLD (IDENT='V1.0', MAIN=HELLO_WORLD,
ADDRESSING_MODE (EXTERNAL=GENERAL)) =
BEGIN
C
#include
C++
#include
C#
class HelloWorldApp {
public static void Main() {
System.Console.WriteLine("Hello, world!");
}
}Clean
module hello
CLIST
PROC 0
WRITE Hello, World!
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
Common Lisp
(format t "Hello world!~%")
DCL command language batch
$ write sys$output "Hello World!"
Eiffel
class HELLO_WORLD
Erlang
-module(hello).
-export([hello_world/0]).Forte TOOL
begin TOOL HelloWorld;
shared=(allow=off, override=on);
transactional=(allow=off, override=on);
monitored=(allow=off, override=on);
distributed=(allow=off, override=on); end class;
-- END CLASS DEFINITIONS
CompatibilityLevel = 0;
ProjectType = APPLICATION;
Restricted = FALSE;
MultiThreaded = TRUE;
Internal = FALSE;
LibraryName = 'hellowor';
StartingMethod = (class = Hello, method = Init); end HelloWorld;
Forth
." Hello, world!" CR
Fortran
PROGRAM HELLO
WRITE(*,10)
10 FORMAT('Hello, world!')
STOP
ENDHaskell
module HelloWorld (main) where
Iptscrae
ON ENTER {
"Hello, " "World!" & SAY
}Io
"Hello world!" print
or write("Hello world!\\n")Java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}Logo
print [hello world!]
Lua
print("Hello, world!")MIXAL
TERM EQU 19 the MIX console device number
ORIG 1000 start address
START OUT MSG(TERM) output data at address MSG
HLT halt execution
MSG ALF "MIXAL"
ALF " HELL"
ALF "O WOR"
ALF "LD "
END START end of the programMSDOS batch
@echo off
echo Hello, world!
OCaml
let _ =
print_endline "Hello world!";;OPL
PROC hello:
PRINT "Hello, World"
ENDPOPS5
(object-class request
^action)
(rule hello
(request ^action hello)
-->
(write |Hello World!| (crlf))
)Pascal
program Hello;
begin
writeln('Hello, world!');
end.Perl
print "Hello, world!\\n";
PHP
Pike
#!/usr/local/bin/pike
int main() {
write("Hello, world!\\n");
return 0;
}PL/I
Test: procedure options(main);
declare My_String char(20) varying initialize('Hello, world!');
put skip list(My_String);
end Test;Python
print "Hello, world!"
REXX, also NetRexx and Object REXX
say "Hello, world!"
Ruby
puts "Hello, world!"
Sather
class HELLO_WORLD is
main is
#OUT+"Hello World\\n";
end;
end;Scheme
(display "Hello, world!")
(newline)
sed (requires at least one line of input)
sed -ne '1s/.*/Hello, world!/p'
Self
'Hello, World!' print.
Smalltalk
Transcript show: 'Hello, world!'
SML
print "Hello, world!\\n";
SNOBOL
OUTPUT = "Hello, world!"
END
SQL
create table MESSAGE (TEXT char(15));
insert into MESSAGE (TEXT) values ('Hello, world!');
select TEXT from MESSAGE;
drop table MESSAGE;Or select 'Hello, world' from dual;
Or, more simply print 'Hello, World.'
Tcl
puts "Hello, world!"
Turing
put "Hello, world!"
UNIX-style shell
echo 'Hello, world!'
Romanian pseudocode (UBB Cluj-Napoca)
Algoritmul Salut este:
fie s:="Hello, world";
tipareste s;
sf-SalutGraphical User Interfaces - as traditional applications
C++ bindings for GTK graphics toolkit
#include
Java
import java.awt.*;
import java.awt.event.*;
Qt toolkit (in C++)
#include
AppleScript
display dialog "Hello, world!"
Visual Basic
MsgBox "Hello, world!"
Windows API (in C)
#include
Graphical User Interfaces - Web browser based
Java applet
JavaScript, aka ECMAScript
Hello World Example
javascript:alert('Hello, world!')XUL
Document Formats
ASCII
48 65 6C 6C 6F 2C 20 77 6F 72 6C 64 21 0D 0A
HTML
PostScript
/Courier findfont 24 scalefont setfont
100 100 moveto
(Hello world!) show
showpage
TeX
\\font\\HW=cmr10 scaled 3000
\\leftline{\\HW Hello world}
\\byeSee also
External links

