Sunday, 6 July 2008

Hello World program examples

Hello World is the first program every user usually codes when he learns a new programming language.

Its goal is learning the basics to edit, compile and execute a program which sends a "hello world" string to standard output.

We have used an Ubuntu Hardy distro based system. Most commands will serve to other distros as well, like Debian.

BASH Shell Scripting Hello World

bash command line interpreter is usually already installed in your system.

if it is not installed we type: $sudo aptitude install bash

Now we create script using whatever text editor we prefer.
e.g: $ gedit

Copy this lines into your script
echo "hello world"

Bash shell scripting is an interpreted language so we have not to compile anything. We directly execute the shell script file:

We give the shell script execution permissions:
$ chmod a+x

and launch the program
$ ./

PYTHON Scripting Hello World

If you do not have python installed in your system, exec:

$ sudo aptitude install python
$ ./configure CFLAGS="-O3"

Create a text file called with your favorite editor and copy these lines:
print "hello world"

Because python is an interpreted language we execute directly the python script:

Add execution permissions:
$ chmod a+x

Execute your hello world python script:
$ ./

C Language Hello World

We need a compiler because C is a compiled language:
$ sudo aptitude install gcc

In order to be able to use standard input output system:
$ sudo aptitude install libc6-dev

Using our favorite text editor, we create hello.c file.

Copy this program within hello.c file:
#include <stdio.h>

main ()
printf("hello world\n");

We compile hello.c file to create hello binary executable file:
$ gcc -o hello hello.c

Add execution permissions:
$ chmod a+x hello

Execute the binary file:
$ ./hello

C++ Language Hello World

First we install the c++ compiler:
$ sudo aptitude install g++

This g++ virtual package installs the g++ compiler and also the c++ standard library:
e.g: g++-4.2, libstdc++6-4.2-dev

Type this file copying this following code:
#include <iostream>

std::cout << "Hello World\n";

We compile program:
$ g++ -o hello

We give execution permissions and execute hello binary file:
$ chmod a+x hello
$ ./hello

JAVA Hello World

Java is a language which is compiled into intermediate code called java bytecodes.
Java bytecodes are executed in a runtime environment whose name is Java Virtual Machine (JVM)

In order to install the Java Virtual Machine
$ sudo aptitude install openjdk-6-jre

and to install javac java compiler.
$ sudo aptitude install openjdk-6-jdk

We create with a text editor a file called

NOTE: File name must be the same as the class, so we will call both HelloWorld

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

Generating java bytecodes:
$ javac -verbose

A file called HelloWorld.class has been generated.

We are going to execute it within the java virtual machine:
$ java HelloWorld


Lisp is an old programming language with a lot of dialects.
One of them is ELISP which is the language emacs editor is written with.

First we open an emacs session:
$ emacs

Now we execute the command eval-expression:
C-: or M-x eval-expression RET

in the minibuffer prompt we write:
(message "hello world") RET

and "hello world" will appear in emacs mini buffer.

In another more program like way we could define a function: (thanks to rspuzio)
We type in the minibuffer prompt:

NOTE: M-: runs eval-expression command.

M-: (defun hi () (message "hello world"))RET

Then to execute the function, also in the minibuffer:
M-: (hi)RET

In another third way we could write a file and execute it as a script:

We create a file named hello.el and paste these lines in it:
(defun hi () (message "Hello World!"))

Then to execute the script:
$ emacs --script hello.el
Hello World!

Qt C++ Hello World

Beginning with Qt: Introduction, Installing Qt Framework, Qt Hello world

2 comentarios:

rspuzio said...

In order to make the LISP example a program like the rest of the examples, instead type the following at the minibuffer prompt:

(defun hi () (message "hello world"))RET

To run the program, type

(hi) RET

Vicente Hernando said...

Hi rspuzio.

Thank you!

I will update the article with your point.