从网上无意看到这个:39种语言编写的Hello World程序,贴出来让大家看看。
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.3 AWK
1.4 BASIC
1.5 BCPL
1.6 Befunge
1.7 Brainfuck
1.8 C
1.9 C++
1.10 C# (C Sharp)
1.11 COBOL
1.12 Common Lisp
1.13 DOS batch
1.14 Eiffel
1.15 Erlang
1.16 Forth
1.17 Fortran
1.18 HQ9+
1.19 INTERCAL
1.20 Java
1.21 MIXAL
1.22 OCaml
1.23 Pascal
1.24 Perl
1.25 PHP
1.26 Pike
1.27 PL/I
1.28 Prolog
1.29 Python
1.30 REXX
1.31 Ruby
1.32 Scheme
1.33 sed (requires at least one line of input)
1.34 Smalltalk
1.35 SNOBOL
1.36 SQL
1.37 Tcl
1.38 Turing
1.39 UNIX-style shell
2 GUI
2.1 Delphi
2.2 Visual Basic
2.3 Visual FoxPro
2.4 X11
2.5 Java
2.6 Java applet
2.7 JavaScript
2.8 PostScript
3 外部链接
--------------------------------------------------------------
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 80x86 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
--------------------------------------------------------------
AWK
BEGIN { print "Hello, world!" }
--------------------------------------------------------------
BASIC
传统版BASIC:
10 PRINT "Hello, world!"
20 END
现代版BASIC:
Print "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!/n");
return 0;
}
--------------------------------------------------------------
C++
#include <iostream.h>
int main()
{
std::cout << "Hello, world!" << std::endl;
}
--------------------------------------------------------------
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 batch
@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!/n").
--------------------------------------------------------------
Forth
." Hello, world!" CR
--------------------------------------------------------------
Fortran
WRITE(*,10)
10 FORMAT('Hello, World!')
STOP
END
--------------------------------------------------------------
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!");
}
}
--------------------------------------------------------------
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
--------------------------------------------------------------
OCaml
let main () =
print_endline "Hello world!";;
--------------------------------------------------------------
Pascal
program Hello;
begin
writeln('Hello, world!');
end.
--------------------------------------------------------------
Perl
#!/usr/local/bin/perl
print "Hello, world!/n";
--------------------------------------------------------------
PHP
<?php
print("Hello, world!");
?>
--------------------------------------------------------------
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;
--------------------------------------------------------------
Prolog
goal
write("hello,world!").
--------------------------------------------------------------
Python
#!/usr/local/bin/python
print "Hello, world!"
--------------------------------------------------------------
REXX
say "Hello, world!"
--------------------------------------------------------------
Ruby
#!/usr/bin/ruby
print "Hello, world!/n"
--------------------------------------------------------------
Scheme
(display "Hello, world!")
(newline)
--------------------------------------------------------------
sed (requires at least one line of input)
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!"
--------------------------------------------------------------
Turing
put "Hello, world!"
--------------------------------------------------------------
UNIX-style shell
#!/bin/sh
echo 'Hello, world!'
--------------------------------------------------------------
GUI
--------------------------------------------------------------
Delphi
program HelloWorld;
uses
Dialogs;
begin
ShowMessage('Hello, World!');
end.
--------------------------------------------------------------
Visual Basic
MsgBox "Hello, world!"
--------------------------------------------------------------
Visual FoxPro
? "Hello, world!"
--------------------------------------------------------------
X11
用一个程序
xmessage 'Hello, world!'
用C++和gtkmm 2
#include <iostream.h>
#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>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
HelloWorld Program says:
<APPLET CODE="HelloWorld.class" WIDTH=600 HEIGHT=100>
</APPLET>
</BODY>
</HTML>
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello, world!", 100, 50);
}
}
--------------------------------------------------------------
JavaScript
JavaScript是一种用于HTML文件的脚本语言。要查看以下程序的运行结果,只要将其复制到任何HTML文本即可。
<script language="javascript">
function helloWorld()
{
javascript: alert("Hello World");
}
</script>
<a href="javascript:this.location()"
οnclick="javascript:helloWorld();">Hello World Example</a>
--------------------------------------------------------------
PostScript
PostScript是一种专门用来创建图像的语言,常用于打印机。
/font /Courier findfont 24 scalefont
font setfont
100 100 moveto
(Hello World!) show
showpage