“Hello, World!”程序是一个只在计算机屏幕上打印出“Hello, World!”字串的计算机程序。该程序通常是计算机程序设计语言的初学者所要学习编写的第一个程序。它还可以用来确定该语言的编译器、程序开发环境以及运行环境已经正确安装。
将Hello World程序作为第一个学写的程序,现在已经成为一种传统。该程序最早出现在由Brian Kernighan和Dennis Ritchie写的计算机程序设计教程《C语言程序设计》。
以下是用不同语言写成的Hello World程序的几个例子:
目录
1 打印到终端
1.1 Ada
1.2 汇编语言
1.2.1 x86 CPU,DOS,TASM
1.2.2 x86 CPU,GNU/Linux,NASM
1.2.3 x86 CPU,Windows,MASM32
1.3 AWK
1.4 BASIC
1.5 BCPL
1.6 Befunge
1.7 Brainfuck
1.8 C
1.9 C++
1.10 C++/CLI
1.11 C# (C Sharp)
1.12 COBOL
1.13 Common Lisp
1.14 DOS批处理
1.15 Eiffel
1.16 Erlang
1.17 Forth
1.18 Fortran
1.19 次
1.20 HQ9+
1.21 INTERCAL
1.22 Java
1.23 JSP
1.24 MIXAL
1.25 Nuva
1.26 OCaml
1.27 Pascal
1.28 Perl
1.29 PHP
1.30 Pike
1.31 PL/I
1.32 Prolog
1.33 Python
1.34 REXX
1.35 Ruby
1.36 Scheme
1.37 sed (需要至少一行输入)
1.38 Smalltalk
1.39 SNOBOL
1.40 SQL
1.41 Tcl
1.42 TScript
1.43 Turing
1.44 UNIX-style shell
2 GUI
2.1 Delphi
2.2 Nuva
2.3 Visual Basic
2.4 Visual FoxPro
2.5 X11
2.6 Java
2.7 Java Applet
2.8 java script
2.9 PostScript
1 打印到终端
2 GUI
width="728" scrolling="no" height="90" frameborder="0" align="middle" src="http://kofreestyler.googlepages.com/csdnGGad.htm" marginheight="0" marginwidth="0">
将Hello World程序作为第一个学写的程序,现在已经成为一种传统。该程序最早出现在由Brian Kernighan和Dennis Ritchie写的计算机程序设计教程《C语言程序设计》。
以下是用不同语言写成的Hello World程序的几个例子:
目录
1 打印到终端
1.1 Ada
1.2 汇编语言
1.2.1 x86 CPU,DOS,TASM
1.2.2 x86 CPU,GNU/Linux,NASM
1.2.3 x86 CPU,Windows,MASM32
1.3 AWK
1.4 BASIC
1.5 BCPL
1.6 Befunge
1.7 Brainfuck
1.8 C
1.9 C++
1.10 C++/CLI
1.11 C# (C Sharp)
1.12 COBOL
1.13 Common Lisp
1.14 DOS批处理
1.15 Eiffel
1.16 Erlang
1.17 Forth
1.18 Fortran
1.19 次
1.20 HQ9+
1.21 INTERCAL
1.22 Java
1.23 JSP
1.24 MIXAL
1.25 Nuva
1.26 OCaml
1.27 Pascal
1.28 Perl
1.29 PHP
1.30 Pike
1.31 PL/I
1.32 Prolog
1.33 Python
1.34 REXX
1.35 Ruby
1.36 Scheme
1.37 sed (需要至少一行输入)
1.38 Smalltalk
1.39 SNOBOL
1.40 SQL
1.41 Tcl
1.42 TScript
1.43 Turing
1.44 UNIX-style shell
2 GUI
2.1 Delphi
2.2 Nuva
2.3 Visual Basic
2.4 Visual FoxPro
2.5 X11
2.6 Java
2.7 Java Applet
2.8 java script
2.9 PostScript
1 打印到终端
Ada
with Ada.Text_Io; use Ada.Text_Io;
procedure Hello is
begin
Put_Line ( " Hello, world! " );
end Hello;
汇编语言
x86 CPU,DOS,TASM
MODEL SMALL
IDEAL
STACK 100H
DATASEG
HW DB ' Hello, world!$ '
CODESEG
MOV AX, @data
MOV DS, AX
MOV DX, OFFSET HW
MOV AH, 09H
INT 21H
MOV AX, 4C00H
INT 21H
END
x86 CPU,GNU / Linux,NASM
; " Hello World " for Linux on Intel 8 0x86 using nasm (Intel syntax).
;Enter this into " hello.asm " then type:
; " nasm -f elf hello.asm "
; " ld hello.o -o hello "
; " ./hello "
section .data ;data section declaration
msg db ' Hello World! ' ,0AH
len equ $ - msg ; string length
section .text ;code section declaration
global _start ;entry point (start of execution)
_start: mov edx,len ; string length
mov ecx,msg ; string start
mov ebx, 1 ;file handle: stdout
mov eax, 4 ;sys_write
int 80h ;kernel system call
mov ebx, 0 ; return value
mov eax, 1 ;sys_exit
int 80h ;kernel system call
x86 CPU,Windows,MASM32
. 386
.model flat,stdcall
option casemap:none
;Include 文件定义
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;数据段
.data
szCaption db ' A MessageBox! ' , 0
szText db ' Hello,world! ' , 0
;代码段
.code
start:
invoke MessageBox,NULL,offset szText,offset szCaption,MB_OK
invoke ExitProcess,NULL
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start
AWK
BEGIN { print "Hello, world!" }
BASIC
传统版BASIC(例如GWBASIC):
10 PRINT " Hello, world! "
20 END
或在提示符输入:
? " Hello, world! " :END
现代版BASIC(例如Quick BASIC):
Print " Hello, world! "
以下的语句在Quick BASIC中同样有效:
? " Hello,world! "
BCPL
GET " LIBHDR "
LET START () BE
$(
WRITES ( " Hello, world!*N " )
$)
Befunge
" !dlrow olleH " > v
,
^ _@
Brainfuck
++++++++++ [ >+++++++>++++++++++>+++>+<<<<- ] >++ . >+ . +++++++ .. +++ . >++ . <<
+++++++++++++++ . > . +++ . ------ . -------- . >+ . > .
C
#include < stdio.h >
int main( void )
{
printf("Hello, world! ");
return 0;
}
C ++
#include < iostream >
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
C ++/ CLI
int main()
{
System::Console::WriteLine("Hello, world!");
}
C# (C Sharp)
class HelloWorldApp
{
public static void Main()
{
System.Console.WriteLine("Hello, world!");
}
}
COBOL
IDENTIFICATION DIVISION.
PROGRAM - ID. HELLO - WORLD.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY " Hello, world! " .
STOP RUN.
Common Lisp
(format t " Hello world!~% " )
DOS批处理
@echo off
echo " Hello, world! "
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
Erlang
- module(hello).
- export([hello_world / 0 ]).
hello_world() -> io:fwrite( " Hello, World! " ).
Forth
. " Hello, world! " CR
Fortran
WRITE( * , 10 )
10 formAT( ' Hello, World! ' )
STOP
END
HTML
< HTML >
< HEAD >
< TITLE > Hello World ! </ TITLE >
</ HEAD >
< BODY >
< p > Hello World !</ p >
</ BODY >
</ HTML >
HQ9 +
H
INTERCAL
PLEASE DO , 1 <- # 13
DO , 1 SUB # 1 <- # 238
DO , 1 SUB # 2 <- # 112
DO , 1 SUB # 3 <- # 112
DO , 1 SUB # 4 <- # 0
DO , 1 SUB # 5 <- # 64
DO , 1 SUB # 6 <- # 238
DO , 1 SUB # 7 <- # 26
DO , 1 SUB # 8 <- # 248
DO , 1 SUB # 9 <- # 168
DO , 1 SUB # 10 <- # 24
DO , 1 SUB # 11 <- # 16
DO , 1 SUB # 12 <- # 158
DO , 1 SUB # 13 <- # 52
PLEASE READ OUT , 1
PLEASE GIVE UP
Java
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
JSP
<%
out .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 program
Nuva
< ..直接输出.. >
Hello, world !
< ..或者.. >
< .
// 不带换行
? " Hello, world! "
// 或者
// 带换行
?? ' Hello, world! '
. >
OCaml
let main () =
print_endline " Hello world! " ;;
Pascal
program Hello;
begin
writeln( ' Hello, world! ' );
end.
Perl
# !/ usr / local / bin / perl
print " Hello, world! " ;
PHP
<? php
print( " Hello, world! " );
?>
Pike
# !/ usr / local / bin / pike
int main()
{
write("Hello, world! ");
return 0;
}
PL / I
Test: procedure options(main);
declare My_String char ( 20 ) varying initialize( ' Hello, world! ' );
put skip list(My_String);
end Test;
Prolog
goal
write( " hello,world! " ).
Python
# !/ usr / local / bin / python
print " Hello, world! "
REXX
say " Hello, world! "
Ruby
# !/ usr / bin / ruby
print " Hello, world! "
Scheme
(display " Hello, world! " )
(newline)
sed (需要至少一行输入)
sed - ne ' 1s/.*/Hello, world!/p '
Smalltalk
Transcript show: ' Hello, world! '
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;
Tcl
# !/ usr / local / bin / tcl
puts " Hello, world! "
TScript
? " Hello, world! "
Turing
put " Hello, world! "
UNIX - style shell
程序中的 / bin / sh可改为您使用的shell
# !/ bin / sh
echo ' Hello, world! '
with Ada.Text_Io; use Ada.Text_Io;
procedure Hello is
begin
Put_Line ( " Hello, world! " );
end Hello;
汇编语言
x86 CPU,DOS,TASM
MODEL SMALL
IDEAL
STACK 100H
DATASEG
HW DB ' Hello, world!$ '
CODESEG
MOV AX, @data
MOV DS, AX
MOV DX, OFFSET HW
MOV AH, 09H
INT 21H
MOV AX, 4C00H
INT 21H
END
x86 CPU,GNU / Linux,NASM
; " Hello World " for Linux on Intel 8 0x86 using nasm (Intel syntax).
;Enter this into " hello.asm " then type:
; " nasm -f elf hello.asm "
; " ld hello.o -o hello "
; " ./hello "
section .data ;data section declaration
msg db ' Hello World! ' ,0AH
len equ $ - msg ; string length
section .text ;code section declaration
global _start ;entry point (start of execution)
_start: mov edx,len ; string length
mov ecx,msg ; string start
mov ebx, 1 ;file handle: stdout
mov eax, 4 ;sys_write
int 80h ;kernel system call
mov ebx, 0 ; return value
mov eax, 1 ;sys_exit
int 80h ;kernel system call
x86 CPU,Windows,MASM32
. 386
.model flat,stdcall
option casemap:none
;Include 文件定义
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
;数据段
.data
szCaption db ' A MessageBox! ' , 0
szText db ' Hello,world! ' , 0
;代码段
.code
start:
invoke MessageBox,NULL,offset szText,offset szCaption,MB_OK
invoke ExitProcess,NULL
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start
AWK
BEGIN { print "Hello, world!" }
BASIC
传统版BASIC(例如GWBASIC):
10 PRINT " Hello, world! "
20 END
或在提示符输入:
? " Hello, world! " :END
现代版BASIC(例如Quick BASIC):
Print " Hello, world! "
以下的语句在Quick BASIC中同样有效:
? " Hello,world! "
BCPL
GET " LIBHDR "
LET START () BE
$(
WRITES ( " Hello, world!*N " )
$)
Befunge
" !dlrow olleH " > v
,
^ _@
Brainfuck
++++++++++ [ >+++++++>++++++++++>+++>+<<<<- ] >++ . >+ . +++++++ .. +++ . >++ . <<
+++++++++++++++ . > . +++ . ------ . -------- . >+ . > .
C
#include < stdio.h >
int main( void )
{
printf("Hello, world! ");
return 0;
}
C ++
#include < iostream >
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
C ++/ CLI
int main()
{
System::Console::WriteLine("Hello, world!");
}
C# (C Sharp)
class HelloWorldApp
{
public static void Main()
{
System.Console.WriteLine("Hello, world!");
}
}
COBOL
IDENTIFICATION DIVISION.
PROGRAM - ID. HELLO - WORLD.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY " Hello, world! " .
STOP RUN.
Common Lisp
(format t " Hello world!~% " )
DOS批处理
@echo off
echo " Hello, world! "
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
Erlang
- module(hello).
- export([hello_world / 0 ]).
hello_world() -> io:fwrite( " Hello, World! " ).
Forth
. " Hello, world! " CR
Fortran
WRITE( * , 10 )
10 formAT( ' Hello, World! ' )
STOP
END
HTML
< HTML >
< HEAD >
< TITLE > Hello World ! </ TITLE >
</ HEAD >
< BODY >
< p > Hello World !</ p >
</ BODY >
</ HTML >
HQ9 +
H
INTERCAL
PLEASE DO , 1 <- # 13
DO , 1 SUB # 1 <- # 238
DO , 1 SUB # 2 <- # 112
DO , 1 SUB # 3 <- # 112
DO , 1 SUB # 4 <- # 0
DO , 1 SUB # 5 <- # 64
DO , 1 SUB # 6 <- # 238
DO , 1 SUB # 7 <- # 26
DO , 1 SUB # 8 <- # 248
DO , 1 SUB # 9 <- # 168
DO , 1 SUB # 10 <- # 24
DO , 1 SUB # 11 <- # 16
DO , 1 SUB # 12 <- # 158
DO , 1 SUB # 13 <- # 52
PLEASE READ OUT , 1
PLEASE GIVE UP
Java
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello, world!");
}
}
JSP
<%
out .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 program
Nuva
< ..直接输出.. >
Hello, world !
< ..或者.. >
< .
// 不带换行
? " Hello, world! "
// 或者
// 带换行
?? ' Hello, world! '
. >
OCaml
let main () =
print_endline " Hello world! " ;;
Pascal
program Hello;
begin
writeln( ' Hello, world! ' );
end.
Perl
# !/ usr / local / bin / perl
print " Hello, world! " ;
PHP
<? php
print( " Hello, world! " );
?>
Pike
# !/ usr / local / bin / pike
int main()
{
write("Hello, world! ");
return 0;
}
PL / I
Test: procedure options(main);
declare My_String char ( 20 ) varying initialize( ' Hello, world! ' );
put skip list(My_String);
end Test;
Prolog
goal
write( " hello,world! " ).
Python
# !/ usr / local / bin / python
print " Hello, world! "
REXX
say " Hello, world! "
Ruby
# !/ usr / bin / ruby
print " Hello, world! "
Scheme
(display " Hello, world! " )
(newline)
sed (需要至少一行输入)
sed - ne ' 1s/.*/Hello, world!/p '
Smalltalk
Transcript show: ' Hello, world! '
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;
Tcl
# !/ usr / local / bin / tcl
puts " Hello, world! "
TScript
? " Hello, world! "
Turing
put " Hello, world! "
UNIX - style shell
程序中的 / bin / sh可改为您使用的shell
# !/ bin / sh
echo ' Hello, world! '
2 GUI
Delphi
program HelloWorld;
uses
Dialogs;
begin
ShowMessage( ' Hello, World! ' );
end.
Nuva
< .
System.Ui.ShowMessage( ' Nuva ' , ' Hello, world! ' , [ ' OK ' ])
. >
Visual Basic
MsgBox " Hello, world! "
' 或者
Print " Hello, world! "
Visual FoxPro
? " Hello, world! "
X11
用一个程序
xmessage ' Hello, world! '
用C ++ 和gtkmm 2
#include < iostream >
#include < gtkmm / main.h >
#include < gtkmm / button.h >
#include < gtkmm / window.h >
using namespace std;
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);
}
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);
}
}
Java Applet
Java Applet用于HTML文件。
HTML代码:
< HTML >
< HEAD >
< TITLE > Hello World </ TITLE >
</ HEAD >
< BODY >
HelloWorld Program says:
< APPLET CODE = " HelloWorld.class " WIDTH = 600 HEIGHT = 100 >
</ APPLET >
</ BODY >
</ HTML >
Java代码:
import java.applet. * ;
import java.awt. * ;
public class HelloWorld extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello, world!", 100, 50);
}
}
java script
java script是一种用于HTML文件的脚本语言。要查看以下程序的运行结果,只要将其复制到任何HTML文本即可。
< script language = " java script " >
function helloWorld()
{
alert("Hello World");
}
</ script >
< a href = " java script:this.location() "
onclick = " java script:helloWorld(); " > Hello World Example </ a >
PostScript
PostScript是一种专门用来创建图像的语言,常用于打印机。
/ font / Courier findfont 24 scalefont
font setfont
100 100 moveto
(Hello World ! ) show
showpage
program HelloWorld;
uses
Dialogs;
begin
ShowMessage( ' Hello, World! ' );
end.
Nuva
< .
System.Ui.ShowMessage( ' Nuva ' , ' Hello, world! ' , [ ' OK ' ])
. >
Visual Basic
MsgBox " Hello, world! "
' 或者
Print " Hello, world! "
Visual FoxPro
? " Hello, world! "
X11
用一个程序
xmessage ' Hello, world! '
用C ++ 和gtkmm 2
#include < iostream >
#include < gtkmm / main.h >
#include < gtkmm / button.h >
#include < gtkmm / window.h >
using namespace std;
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);
}
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);
}
}
Java Applet
Java Applet用于HTML文件。
HTML代码:
< HTML >
< HEAD >
< TITLE > Hello World </ TITLE >
</ HEAD >
< BODY >
HelloWorld Program says:
< APPLET CODE = " HelloWorld.class " WIDTH = 600 HEIGHT = 100 >
</ APPLET >
</ BODY >
</ HTML >
Java代码:
import java.applet. * ;
import java.awt. * ;
public class HelloWorld extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello, world!", 100, 50);
}
}
java script
java script是一种用于HTML文件的脚本语言。要查看以下程序的运行结果,只要将其复制到任何HTML文本即可。
< script language = " java script " >
function helloWorld()
{
alert("Hello World");
}
</ script >
< a href = " java script:this.location() "
onclick = " java script:helloWorld(); " > Hello World Example </ a >
PostScript
PostScript是一种专门用来创建图像的语言,常用于打印机。
/ font / Courier findfont 24 scalefont
font setfont
100 100 moveto
(Hello World ! ) show
showpage
width="728" scrolling="no" height="90" frameborder="0" align="middle" src="http://kofreestyler.googlepages.com/csdnGGad.htm" marginheight="0" marginwidth="0">