Jump to content

"Hello, World!" program

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Cccp3 (talk | contribs) at 09:52, 4 May 2014 (→‎History: added link to "ASCII"). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A GUI "Hello World" program, written in Perl
CNC machining test in Perspex

A "Hello world" program is a computer program that outputs "Hello, world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language. It is also used to verify that a language or system is operating correctly.

In a device that does not display text, a simple program to produce a signal, such as turning on an LED, is often substituted for "Hello world" as the introductory program.

Purpose

A "Hello World" program has become the traditional first program that many people learn. In general, it is simple enough so that people who have no experience with computer programming can easily understand it, especially with the guidance of a teacher or a written guide. Using this simple program as a basis, computer science principles or elements of a specific programming language can be explained to novice programmers. Experienced programmers learning new languages can also gain a lot of information about a given language's syntax and structure from a hello world program.

In addition, hello world can be a useful sanity test to make sure that a language's compiler, development environment, and run-time environment are correctly installed. Configuring a complete programming toolchain from scratch to the point where even trivial programs can be compiled and run can involve substantial amounts of work. For this reason, a simple program is used first when testing a new tool chain.

A "Hello World" program running on Sony's PlayStation Portable as a proof of concept.

"Hello world" is also used by computer hackers as a proof of concept that arbitrary code can be executed through an exploit where the system designers did not intend code to be executed—for example, on Sony's PlayStation Portable. This is the first step in using homemade content ("home brew") on such a device.

History

While small test programs existed since the development of programmable computers, the tradition of using the phrase "Hello, world!" as a test message was influenced by an example program in the seminal book The C Programming Language. The example program from that book prints "hello, world" (without capital letters or exclamation mark), and was inherited from a 1974 Bell Laboratories internal memorandum by Brian Kernighan, Programming in C: A Tutorial,[1] which contains the first known version:

 main()
 {
        printf("hello, world");
 }

The C version was adapted from Kernighan's 1972 A Tutorial Introduction to the Language B,[2] where the first known version of the program is found in an example used to illustrate external variables:

 main(){
   extrn a,b,c;
   putchar(a); putchar(b); putchar(c); putchar('!*n');
   }

 a 'hell';
 b 'o, w';
 c 'orld';

The program prints "hello, world!" on the terminal, including a newline character. The phrase is divided into multiple variables because in B, a character constant is limited to four ASCII characters. The previous example in the tutorial printed "hi!" on the terminal, so the phrase "hello, world!" was originally introduced as a slightly longer greeting that required several character constants for its expression.

It is also claimed that "hello, world" originated instead with BCPL (1967).[3]

For modern languages, the hello world program tends to subtly grow in sophistication. For example, the Go programming language introduced a multilingual hello world program,[4] Sun demonstrated a Java hello world based on scalable vector graphics,[5] and the XL programming language features a spinning Earth hello world using 3D graphics.[6]

Variations

There are many variations on the punctuation and casing of the phrase. Variations include the presence or absence of the comma and exclamation mark, and the capitalization of the 'H', both the 'H' and the 'W', or neither. Some languages are forced to implement different forms, such as "HELLO WORLD!", on systems that support only capital letters, while many "hello world" programs in esoteric languages print out a slightly modified string. For example, the first non-trivial Malbolge program printed "HEllO WORld", this having been determined to be good enough.

There are variations in spirit, as well. Functional programming languages, like Lisp, ML and Haskell, tend to substitute a factorial program for Hello World, as functional programming emphasizes recursive techniques, whereas the original examples emphasize I/O, which violates the spirit of pure functional programming by producing side effects.

The Debian and Ubuntu Linux distributions provide the "hello world" program through the apt packaging system; this allows users to simply type "apt-get install hello" for the program to be installed, along with any software dependencies. While of itself useless, it serves as a sanity check and a simple example to newcomers of how to install a package. It is significantly more useful for developers, however, as it provides an example of how to create a .deb package, either traditionally or using debhelper, and the version of hello used, GNU hello, serves as an example of how to write a GNU program.[7]

Examples

The Hello World program can be executed in many different programming languages, and in many different ways. The simplest ways are generally to just create a print line, or a String containing the words "Hello World!", however, one can use substrings and many other variations of programming in order to execute the Hello World program creation. Some languages such as Java may require several lines of code, while others such as Python or Ruby may need only a single statement.

In Io:

"Hello World!" println

In Java:

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

In JavaScript:

alert("Hello World!");

In PHP:

<?php echo "Hello World!"; ?>

In Python:

print("Hello World!")

In Ruby:

puts "Hello World!"

In Assembly language:

%TITLE "HELLO.COM"
; 16-bit Com file.
; Written in TASM assembly.
; Written for the Intel 8086 processor, and should work on any x86 processor.

.Model Tiny

.Data
    Hello DB 0Dh, 0Ah, 'Hello, World!', 00h

.Code
.8086
    Org 100h

Start:
    Push ax
    Push bx
    Push si

    Lea si, HELLO
    Mov al, [si]

PRINT_MSG:
    Cmp al, 00h
    Jz DONE

    Mov ah, 0Eh
    Mov al, [si]
    Mov bh, 00h
    Int 10h

    Inc si

    Jmp short PRINT_MSG

Done:
    Mov ah, 00h
    Int 16h

    Pop si
    Pop bx
    Pop ax
    Int 20h

End Start
End PRINT_MSG
End Done
.End

Alternatively, if the screen does not have a textual output, a flash or blink of the device's light can be used as a different method, with the same meaning behind it. This is often used for Arduino devices, where pin 13 typically has a board-mounted LED fitted as standard.

See also

References

  1. ^ "Programming in C: A Tutorial" (PDF).
  2. ^ "The Programming Language B" (PDF).
  3. ^ BCPL, Jargon File
  4. ^ A Tutorial for the Go Programming Language. The Go Programming Language. Retrieved July 26, 2011.
  5. ^ Jolif, Christophe (January 2003). "Bringing SVG Power to Java Applications". Sun Developer Network.
  6. ^ de Dinechin, Christophe (July 24, 2010). "Hello world!". Grenouille Bouillie.
  7. ^ List of Hello World Programs in 200 Programming Languages. Scriptol.com

External links