Misplaced Pages

"Hello, World!" program

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

This is an old revision of this page, as edited by Ap (talk | contribs) at 16:08, 7 February 2003. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Revision as of 16:08, 7 February 2003 by Ap (talk | contribs)(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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.

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:

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
   10 PRINT "Hello, world!"
   20 END
   GET "LIBHDR"
   LET START () BE
   $(
       WRITES ("Hello, world!*N")
   $)
   ++++++++++>++.>+.+++++++..+++.>++.<<
   +++++++++++++++.>.+++.------.--------.>+.>.
   #include <stdio.h>
   int main(void) {
       printf("Hello, world!\n");
       return 0;
   }
   #include <iostream>
   using namespace std;
   int main() {
       cout << "Hello, world!" << endl;
   }
   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
   ."Hello, world!"
   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!");
       }
   }
   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!"
   #!/usr/bin/ruby
   print "Hello, world!"
  • sed (requires at least one line of input)
   sed -ne '1s/.*/Hello, world!/p'
   Transcript show: 'Hello, world!'
   #!/usr/local/bin/tcl
   puts "Hello, world!"
   put "Hello, world!"
   #!/bin/sh
   echo 'Hello, world!'
   (display "Hello, world!")
   (newline)
    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

Graphical User Interfaces

   MsgBox "Hello, world!"

X11

using a program

   xmessage 'Hello, world!'

using C++ and 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 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

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>

External Link

See also: Just another Perl hacker