Hello, World!

07.24.2007, Librarys, by , 1,259 views.

“Hello, World!” 程序是一个在计算机屏幕上只打印出 “Hello, World!”(意为“世界,你好!”)的程序。像这样简单的程序, 通常是计算机程序设计语言的初学者, 在学习编写时的第一个程序。

这也是计算机发展史上最为经典的程序,它标志着你已经写出了第一个可以运行的程序了。

把印出 “Hello World” 作为第一个学写的程序,现在已经成为一种传统。该程序最早出现在由 Brian Kernighan 和 Dennis Ritchie 写的计算机程序设计教程《C语言程序设计》代码是:

#include <stdio.h>

int main(int argc, char* argv[])

{

printf(“Hello, world!n”);

return 0;

}

或者:

#include <stdio.h>

int main(void)

{puts(“Hello, world!”);}

那么如果把编程语言中的的”Hello, World!”程序都统计了是怎样的状况呢?

以下资料来自维基百科

——

ActionScript

trace(“Hello, world!”);

Ada

with TEXT_IO;

procedure HELLO is

begin

TEXT_IO.PUT_LINE (“Hello, world!”);

end HELLO;

汇编语言

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

x86 CPU、Windows、MASM32

.386

.model flat,stdcall

option casemap:none

;==========================================================

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, addr szText, addr szCaption, MB_OK

invoke ExitProcess, NULL

;==========================================================

end start

ASP

<% Response.Write(“Hello, world!”) %>

或者:

<% strHelloWorld = “Hello, world!” %>

<%= strHelloWorld %>

或者简单地写成:

<%= “Hello, world!” %>

AutoIt

MsgBox(1,”,’Hello, world!’)

AWK

BEGIN { print “Hello, world!” }

Bash (or sh)

echo ‘Hello, world!’

或者:

printf ‘Hello, world!n’

BASIC

传统版 BASIC(例如 GWBASIC):

10 PRINT “Hello, world!”

20 END

或在提示符输入:

? “Hello, world!”

现代版 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

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<

+++++++++++++++.>.+++.——.——–.>+.>.

BlitzBasic

Print “Hello, world!”

WaitKey

C

#include <stdio.h>

int main(int argc, char* argv[])

{

printf(“Hello, world!n”);

return 0;

}

或者:

#include <stdio.h>

int main(void)

{puts(“Hello, world!”);}

C++

#include <iostream>

int main()

{

std::cout << “Hello, world!” << std::endl;

return 0;

}

或者:

#include <iostream>

using namespace std;

int main()

{

cout << “Hello, world!” << endl;

return 0;

}

C++/CLI

int main()

{

System::Console::WriteLine(“Hello, world!”);

}

C# (C Sharp)

class HelloWorldApp

{

static void Main()

{

System.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!”);

}

}

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 Hello, world!

对于MS-DOS 3.0或更低版本:

echo off

cls

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

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

%>

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 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!n”;

PHP

<?php

echo ‘Hello, world!’;//通常使用这个, 比 print 少一个字符

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

(需要至少一行输入)

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

Seed7

$ include “seed7_05.s7i”;

const proc: main is func

begin

writeln(“Hello, world!”);

end func;

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!’

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

}

}

JavaScript

JavaScript是一种用于HTML文件的脚本语言。要查看以下程序的运行结果,只要将其复制到任何HTML文本即可。

<script language=”javascript”>

function helloWorld()

{

alert(“Hello World”);

}

</script>

<a href=”javascript:this.location()”

onclick=”javascript:helloWorld();”>Hello World Example</a>

PostScript

PostScript是一种专门用来创建图像的语言,常用于打印机。

/font /Courier findfont 24 scalefont

font setfont

100 100 moveto

(Hello World!) show

showpage

XAML

<Page

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

x:Class=”XAMLSample.Page1″

>

<Button Click=”HelloWorld” Name=”Button1″>Click Here</Button>

</Page>

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

}

}

}

1个评论

  1. kkk

     够经典的了