<<Up     Contents

Hello world program

Redirected from Hello world

A "hello world" program is a computer program that simply prints out "Hello, world!".

This is a traditional first program to write when learning a new programming language, and can be a useful sanity test to make sure that a language's compiler, development environment[?], and run-time environment are correctly installed.

Although this is a trivial exercise in a system where all of these are pre-configured, 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 minimal test programs such as this existed since the development of programmable computers, the tradition of using "Hello, world!" as the test message was probably started by its use as an example program in the book The C Programming Language, by Brian Kernighan and Dennis Ritchie.

Here are some examples in different languages:

Table of contents
1 External Link

Console

    with Ada.Text_Io; use Ada.Text_Io;
    procedure Hello is
    begin
       Put_Line ("Hello, world!");
    end Hello;

    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

    BEGIN { print "Hello, world!" }

    Traditional - Unstructured BASIC
    10 PRINT "Hello, world!"
    20 END

    More modern versions - Structured BASIC
    print "Hello, world!"

    GET "LIBHDR"

    LET START () BE
    $(
        WRITES ("Hello, world!*N")
    $)

 "!dlrow olleH">v
                ,
               ^_@

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

    #include <stdio.h>

    int main(void) {
        printf("Hello, world!\n");
        return 0;
    }

    #include <iostream>
    using namespace std;

    int main() {
        cout << "Hello, world!" << endl;
        return 0;
    }

    class HelloWorldApp {
     public static void Main() {
        System.Console.WriteLine("Hello, world!");
     }
    }

    IDENTIFICATION DIVISION.
    PROGRAM-ID.     HELLO-WORLD.

    ENVIRONMENT DIVISION.

    DATA DIVISION.

    PROCEDURE DIVISION.
    DISPLAY "Hello, world!".
    STOP RUN.

    (format t "Hello world!~%")

    @echo off
    echo "Hello, world!"

    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

        -module(hello).
        -export([hello_world/0]).

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

    ." Hello, world!" CR

       WRITE(*,10)
    10 FORMAT('Hello, World!')
       STOP
       END

    H

    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

    public class Hello {
        public static void main(String[] args) {
            System.out.println("Hello, world!");
        }
    }

    print "Hello, world!"

     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

    let main () =
       print_endline "Hello world!";;

    program Hello;
    begin
        writeln('Hello, world!');
    end.

    #!/usr/local/bin/perl
    print "Hello, world!\n";

    <?php
        print("Hello, world!");
    ?>

    #!/usr/local/bin/pike
    int main() {
        write("Hello, world!\n");
        return 0;
    }

    Test: procedure options(main);
       declare My_String char(20) varying initialize('Hello, world!');
       put skip list(My_String);
    end Test;

    #!/usr/local/bin/python
    print "Hello, world!"

    say "Hello, world!"

    #!/usr/bin/ruby
    print "Hello, world!\n"

    (display "Hello, world!")
    (newline)

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

    Transcript show: 'Hello, world!'

        OUTPUT = "Hello, world!"
    END

    create table MESSAGE (TEXT char(15));
    insert into MESSAGE (TEXT) values ('Hello, world!');
    select TEXT from MESSAGE;
    drop table MESSAGE;

    #!/usr/local/bin/tcl
    puts "Hello, world!"

    put "Hello, world!"

    #!/bin/sh
    echo 'Hello, world!'

Graphical User Interfaces

    MsgBox "Hello, world!"

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

    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 applets[?] work in conjunction with HTML files.

    <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 is a scripting language used in HTML files. To demo this program Cut and Paste the following code into any HTML file.

    <script language="javascript">
    function helloWorld()
    {
        javascript: alert("Hello World");
    }
    </script>

    <a href="javascript:this.location()"
     onclick="javascript:helloWorld();">Hello World Example</a>

An easier method uses JavaScript implicitly, calling the reserved alert function. Cut and paste the following line inside the <BODY> .... </BODY> HTML tags.

    <a href="#" onclick="alert('Hello World')">Hello World Example</a>

 <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 <box align="center">
 <label value="Hello, world!" />
 </box>
 </window>

Document Formats

 <HTML>
 <HEAD>
 <TITLE>Hello, world!</TITLE>
 </HEAD>
 <BODY>
 Hello, world!
 </BODY>
 </HTML>

 /font /Courier findfont 24 scalefont
 font setfont
 100 100 moveto
 (Hello World!) show
 showpage

"Hello World" CS In-Joke

Non-CS Manager

  mail -s "Hello, world." joe@b12
  Joe, could you please write me a program that prints "Hello,
 world."?
  I need it by tomorrow.
  ^D

Non-CS Senior Manager

  % zmail jim
  I need a "Hello, world." program by this afternoon.
 
Chief Executive
  % letter
  letter: Command not found.
  % mail
  To: ^X ^F ^C
  % help mail
  help: Command not found.
  % damn!
  !: Event unrecognized
  % logout

See also:

External Link

wikipedia.org dumped 2003-03-17 with terodump