Hello World 程式樣例

以下是用不同程式語言寫成的 Hello World 程式的列表：

列印到終端

1.1 ActionScript

trace ("Hello, world!");

1.2 Ada

with TEXT_IO;

procedure HELLO is

begin

TEXTIO.PUTLINE ("Hello, world!");

end HELLO;

1.3 組合語言

1.3.1 x86 CPU，GNU/Linux，NASM

section .data

msg db 'Hello, world!',0xA

len equ $-msg

section .text

global _start

_start:

mov edx,len

mov ecx,msg

mov ebx,1

mov eax,4

int 0x80

mov ebx,0

mov eax,1

int 0x80

1.3.2 x86 AT&T、gas

.data

msg : .string "Hello, world!\n"

len = . - msg

.text

.global _start

_start:

movl $len, % edx

movl $msg, % ecx

movl $1 , % ebx

movl $4 , % eax

int $0x80

movl $0 , % ebx

movl $1 , % eax

int $0x80

1.3.3 x86 CPU、Windows、MASM32

.386

.model flat,stdcall

option casemap:none

1.3.3.1 =

include windows.inc

include user32.inc

includelib user32.lib

include kernel32.inc

includelib kernel32.lib

1.3.3.2 =

.data

szCaption db "A MessageBox!", 0

szText db "Hello, world!", 0

1.3.3.3 =

.code

start:

invoke MessageBox, NULL, addr szText, addr szCaption, MB_OK

invoke ExitProcess, NULL

1.3.3.4 =

end start

1.3.4 8086 作業系統（NASM）

BITS 16

org 0x7c00

mov ax,cs

mov ds,ax

mov es,ax

call DispStr

jmp $;End Hear

DispStr:

mov ax, BootMessage

mov bp, ax

mov cx, 16;How long is the String

mov ax, 0x1301

mov bx, 0x000c

mov dl, 0

int 0x10

ret

BootMessage: db "Hello, world!"

times 510-($-$$) db 0x0

dw 0xaa55; Bootable Mark

1.4 AutoIt

MsgBox (1,,'Hello, world!')

1.5 AWK

BEGIN {print "Hello, world!"}

1.6 Bash (或其他 Unix Shell)

echo 'Hello, world!'

或者：

printf 'Hello, world!\n'

1.7 BASIC

傳統版 BASIC（例如 GWBASIC）：

10 PRINT "Hello, world!"

20 END

或：

10 PRINT "Hello, world!"

或在提示符輸入：

? "Hello, world!"

現代版 BASIC（例如 Quick BASIC）：

Print "Hello, world!"

以下的語句，在 Quick BASIC 中同樣有效：

? "Hello, world!"

1.8 BCPL

GET "LIBHDR"

LET START BE

$(WRITES ("Hello, world!*N")

$)

1.9 Brainfuck

++++++++++>+++++++>++++++++++>+++>+++.>+.+++++++..+++.>++..+++.------.--------.>+.>.

1.10 Blitz Basic

Print "Hello, world!"

WaitKey

1.11 BOO

print "Hello, world!"

1.12 C

include

int main (void)

{printf ("Hello, world!\n");

return 0;

}

或者：

include

int main (void)

{puts ("Hello, world!");

return 0;

}

1.13 CoffeeScript

console.log 'Hello, world!'

或者：

alert 'Hello, world!'

1.14 C++

include

int main

{

std::cout

或者：

include

using namespace std;

int main

{

cout

1.15 C++/CLI

int main

{System::Control::WriteLine ("Hello, world!");

}

1.16 C# (C Sharp)

using System

class HelloWorldApp

{static void Main (string args)

{Console.WriteLine ("Hello, world!");

}

}

或者（僅用於 Microsoft Windows）

class HelloWorldApp

{DllImport ("user32.dll")

static extern MessageBox (string title, string message);

public static void Main

{MessageBox (null, "Hello, world!");

}

}

或者（使用附加的 Windows Forms）

using System.Windows.Forms;

class HelloWorldApp

{public static void Main

{MessageBox.Show ("Hello, world!");

}

}

1.17 COBOL

IDENTIFICATION DIVISION.

PROGRAM-ID. HELLO-WORLD.

ENVIRONMENT DIVISION.

DATA DIVISION.

PROCEDURE DIVISION.

DISPLAY "Hello, world!".

STOP RUN.

1.18 Common Lisp

直接輸出

"Hello world!"

或者

(format t "Hello world!~%")

1.19 DOS 批處理

@echo Hello, world!

對於 MS-DOS 3.0 或更低版本：

echo off

cls

echo Hello, world!

1.20 Eiffel

class HELLO_WORLD

creation

make

feature

make is

local

io:BASIC_IO

do

io

io.put_string ("% N Hello, world!")

end -- make

end -- class HELLO_WORLD

1.21 Erlang

-module (hello).

-export (hello_world/0).

hello_world -> io:fwrite ("Hello, World!\n").

1.22 Flowgorithm

}}

file:Flowgorithm Hello World.png

1.23 Forth

."Hello, world!" CR

1.24 Fortran

WRITE (,) 'Hello, world!'

STOP

END

1.25 Go

package main

import "fmt"

func main {fmt.Println ("Hello, world!")

}

1.26 INTERCAL

}}

PLEASE DO ,1

1.27 Java

public class Hello

{public static void main (String args)

{System.out.print ("Hello, world!");

}

}

或者：

class HelloWorld

{public static void main (String args)

{System.out.print ("Hello, world!");

}

}

1.28 JavaScript

該程式碼適用於瀏覽器控制檯以及 Node.js 等伺服器端執行環境。

console.log ("Hello, World!");

1.29 Julia

println ("Hello, world!")

1.30 Lisp

直接輸出

"hello, world"

或者

(format t "hello, world~%")

1.31 Lua

print "Hello, world!"

1.32 Malbolge

(=gY;dts76qKJImZkj

1.33 Matlab

disp ('hello world!')

1.34 Mathematica

Hello := Print"Hello World!"

Hello

1.35 Metapost

beginfig (1);

draw (0,0)--(0,10);

draw (0,5)--(5,5);

draw (5,0)--(5,10);

draw (12,0)--(7,0)--(7,10)--(12,10);

draw (12,5)--(7,5);

draw (14,10)--(14,0)--(19,0);

draw (21,10)--(21,0)--(26,0);

draw (28,5)...(30.5,0)...(33,5)...(30.5,10)...cycle;

draw (38,10)--(39.25,0)--(40.5,10)--(41.75,0)--(43,10);

draw (45,5)...(47.5,0)...(50,5)...(47.5,10)...cycle;

draw (52,0)--(52,10);

draw (52,10)..(57,4)..(52,6.5);

draw (52,5)--(57,0);

draw (61,10)--(61,0)--(66,0);

draw (68,10)--(68,0)..(73,5)..cycle;

endfig;

end

1.36 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 program

1.37 Nuva

Hello, world!

1.38 Objective-C

import

int main (int argc, const char * argv)

{

@autoreleasepool {NSLog (@"Hello, World!");

}

return 0;

}

1.39 OCaml

let main =

print_endline "Hello world!";;

1.40 Pascal

program Hello;{此行可以省略}

begin

writeln ('Hello, world!');

end.

1.41 Perl

!/usr/bin/env perl

print "Hello, world!\n";

Perl 5.10（含）以後版本:

!/usr/bin/env perl

use 5.010;

say "Hello, world!";

1.42 Pike

!/usr/local/bin/pike

int main

{write ("Hello, world!\n");

return 0;

}

1.43 PL/I

Test: procedure options (main);

declare My_String char (20) varying initialize ('Hello, world!');

put skip list (My_String);

end Test;

1.44 Prolog

goal

write ("hello,world!").

1.45 Python

適用於 Python 2：

!/usr/bin/env python

print "Hello, world!"

適用於 Python 2.6, 2.7, 3：

!/usr/bin/env python

print ("Hello, world!")

用彩蛋輸出 Hello World：

!/usr/bin/env python

import hello

1.46 REXX

say "Hello, world!"

1.47 Ruby

!/usr/bin/ruby

puts "Hello, world!"

1.48 Rust

fn main {println!("Hello, world!");

}

1.49 Scheme

(display "Hello, world!")

(newline)

1.50 Scratch

1.51 sed

（需要至少一行輸入）

sed -ne '1s/.*/Hello, world!/p'

1.52 Seed7

}}

$ include "seed7_05.s7i";

const proc: main is func

begin

writeln ("Hello, world!");

end func;

1.53 Smalltalk

Transcript show: 'Hello, world!'

1.54 Small Basic

TextWindow.WriteLine ("Hello, world!")

1.55 SNOBOL

OUTPUT = "Hello, world!"

END

1.56 SQL

1.56.1 第一種

create table MESSAGE (TEXT char (15));

insert into MESSAGE (TEXT) values ('Hello, world!');

select TEXT from MESSAGE;

drop table MESSAGE;

1.56.2 第二種

select 'hello, world';

1.56.3 第三種

print 'hello,world!'

1.57 Swift

適用於 Swift 1.x：

println ("Hello, World!")

適用於 Swift 2.x, 3：

print ("Hello, World!")

1.58 Tcl

!/usr/local/bin/tcl

puts "Hello, world!"

1.59 TScript

}}

? "Hello, world!"

1.60 Turing

}}

put "Hello, world!"

1.61 Unix shell 指令碼

程式中的 /bin/sh 可改為您使用的 shell

!/bin/sh

echo 'Hello, world!'

1.61.1 bc

!/usr/bin/bc -q

print "Hello World"

quit

1.61.2 dc

!/usr/bin/env dc

Hello Worldp

圖形使用者介面

2.1 AppleScript

display dialog "Hello, world!"

或者：

display alert "Hello, world!"

2.2 Delphi

program HelloWorld;

uses

Dialogs;

begin

ShowMessage ('Hello, World!');

end.

2.3 Nuva

2.4 Visual Basic

Sub Main

MsgBox "Hello, world!"

End Sub

2.5 Visual FoxPro

? "Hello, world!"

2.6 X11

用一個程式

xmessage 'Hello, world!'

使用 Qt

include

int main (int argc, char *argv)

{QApplication app (argc, argv);

QLabel label ("Hello, world!");

label.show ;

return app.exec ;}

C 和 GTK+

include

int main (int argc, char * args)

{

GtkWidget win, label;

gtk_init (& argc, & args);

label = gtklabelnew ("Hello, world!");

win = gtkwindownew (GTKWINDOWTOPLEVEL);

gtkcontaineradd (GTK_CONTAINER (win), label);

gtkwidgetshow_all ;

gtk_main ;

return 0;

}

用 C++ 和 gtkmm 2

include

using namespace std;

class HelloWorld : public Gtk::Window

{

public:

HelloWorld ;

virtual ~HelloWorld ;

protected:

Gtk::Button m_button;

virtual void onbuttonclicked ;};

HelloWorld::HelloWorld : m_button ("Hello, world!")

{setborderwidth (10);

mbutton.signalclicked .connect (SigC::slot (*this, &HelloWorld::onbuttonclicked));

add (m_button);

m_button.show ;}

HelloWorld::~HelloWorld

void HelloWorld::onbuttonclicked

{

cout

2.7 Java

import java.awt.*;

import java.awt.event.*;

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);

}

}

2.8 Java Applet

Java Applet 用於 HTML 檔案。

HTML 程式碼：

Hello World

HelloWorld Program says:

Java 程式碼：

import java.applet.*;

import java.awt.*;

public class HelloWorld extends Applet

{public void paint (Graphics g)

{g.drawString ("Hello, world!", 100, 50);

}

}

2.9 JavaScript

JavaScript 是一種指令碼語言。最廣泛用於 HTML 檔案中，也可以用在其它宿主環境下，比如 Microsoft® Windows® 指令碼宿主（WSH）和一些 web 服務環境。

用於 HTML 中：

// 彈出對話方塊

alert ("Hello, World!");

// 在頁面上顯示

document.write ("Hello, World!");

用於 WSH 中：

WScript.Echo ("Hello, World!");

作為伺服器端（Node.js）：啟動後，需要在瀏覽器中訪問 “http://127.0.0.1:8000/” 檢視。

const http = require ('http');

http.createServer ((request, response) => {response.writeHead (200, { 'Content-Type': 'text/plain'});

response.end ('Hello World!');

}).listen (8000);

console.log ('Server running at http://127.0.0.1:8000/');

2.10 PostScript

PostScript 是一種專門用來建立影像的語言，常用於印表機。

/font/Courier findfont 24 scalefont

font setfont

100 100 moveto

(Hello World!) show

showpage

2.11 SPARQL

SELECT ?h WHERE {VALUES ?h { "Hello World"}

}

2.12 XAML

Click Here

接著使用 C# 建置

using System;

using System.Windows;

using System.Windows.Controls;

namespace XAMLSample

{

public partial class Page1 : Page

{void HelloWorld (object sender, RoutedEventArgs e)

{MessageBox.Show ("Hello, world!");

}

}

}

2.13 易語言

. 版本 2

. 程式集 視窗程式集 1

. 子程式 啟動視窗_建立完畢

資訊框 (“Hello, World!”, 0, )

伺服器端

以下為 Web 服務端示例程式，程式啟動後，需要用瀏覽器訪問特定地址來檢視結果。

3.1 ASP

或者簡單地寫成：

3.2 HTML

Hello World

Hello World

Hello World

3.3 Go

package main

import (

"fmt"

"net/http"

)

func helloHandler (w http.ResponseWriter, r *http.Request) {fmt.Fprintf (w, "hello world")

}

func main {http.HandleFunc ("/", helloHandler)

http.ListenAndServe ("0.0.0.0:8000", nil)

}

3.4 JSP

或者簡單地寫成：

3.5 JavaScript

以下為 Node.js 環境：

const http = require ('http');

http.createServer ((request, response) => {response.writeHead (200, { 'Content-Type': 'text/plain'});

response.end ('Hello World!');

}).listen (8000);

console.log ('Server running at http://127.0.0.1:8000/');

3.6 PHP

以下程式碼既可以作為伺服器端執行，顯示在使用者瀏覽器上，也可以在終端中直接執行，輸出到終端中：

或者