Computer Programming: Hello World!

COOL Science Blog

Computer Programming: Hello World!

What is computer programming?

A dictionary [] definition of programming:

  • the planning, scheduling, or performing of a program
  • the process of instructing or learning by means of an instructional program
  • the process of preparing an instructional program for a device (such as a computer)

Computers get instructions to do tasks from programs that are constructed in a programming language (the source code) and then converted to machine code. Those tasks could be to carry out mathematical computations, create a web page, create software such as writing, accounting, drawing applications and much more. The source code is considered human readable but has to be converted (by compilers, assemblers or interpreters depending on the programming language) into a form that is computer readable.

So, whilst it may seem that when we sit down to our computer it knows exactly what to do, for example:

  • when switched on it requests login details and only allows us ‘in’ if they're correct;
  • it keeps track of updates and advises when they are needed;
  • it manages the resources such as memory and processor access needed when we have numerous applications open

There are many more ‘behind the scenes’ processes that computers carry out, they are all hidden from us and probably taken for granted most of the time. But we should remember that someone has actually written those sets of instructions (the programs) that ‘tell’ the computer how to do all of these things; and then take a moment to imagine and appreciate how many thousands upon thousands of lines of code they contain!

"Hello World" - A programmers first words!

The first "Hello World" program is found in “A Tutorial Introduction to the Programming Language B” (Brian W. Kernighan, pub. 1973) which was a forerunner of the book "C Programming Language" (Brian W. Kernighan & Dennis M. Ritchie, first pub. 1978) and it is Brian Kernighan who is credited with it’s creation.

Simply put the “Hello World” program is a relatively basic program used to illustrate the syntax of a particular programming language; of course it ultimately displays the message “Hello World” which gives great joy to any new programmer!

Hello World is even used as a means of gauging the of ease-of-use of a programming language; the time it takes to write and execute a “Hello World” program in a given language is known as the TTHW or “Time to Hello World”

“Hello World” in C++

To write a program in C++ three things are needed: a compiler, a text editor and a command line terminal. If you're on a Linux Box (computer) writing a “Hello World” program in C++ can be a remarkably simple task as it usually has all of these installed and ready to go. If you're on a Windows computer the process is similar but you’ll probably need to install a suitable compiler – there’s plenty of information on how to do this on various websites.

So, here's a quick “Hello World” demonstration in C++ (we’re using a Linux box)
In a text editor (we use Kate) we typed the following:

Code for hello World program shown in text file

This file was saved as HelloWorld.cpp in a folder named HelloWorld; we then opened a command terminal from within that folder and typed the following commands (pressing Enter after each):
g++ HelloWorld.cpp -o test

The result was:

Commands and results shown in command line terminal

As stated before the code shows the basic structure of a program within a particular language, in this case C++, to explain briefly:

  • #include <iostream> instructs the preprocessor to include the content of one of the several libraries included in the C++ compiler
  • int main() All C++ programs have a “main()” function, the ‘int’ section means that this function will return an integer value.
  • { } The curly braces enclose the body of the function, that is the code you want to use to ‘do’ something
  • printf("Hello World"); In this case we are printing Hello World to the terminal screen
  • return 0; and returning the integer 0 (zero)

In the terminal, the command lines are:

  • g++ HelloWorld.cpp -o test
    • g++ is one of several compilers for C++ and its going to take our text file HelloWorld.cpp and compile it into a binary executable (more on that below!).
    • -o tells the compiler to output the executable file and give it a name; in this case we just used ‘test ‘ but it could be any name. After running this command a file called ‘test’ does indeed appear in the HelloWorld folder.
  • ./test this command runs the code in the test file giving the result as shown in the image above.

Although this may not look all that impressive if it’s your first go at programming you've just written, compiled and ran a C++ program!

But the output is a bit untidy, our “Hello World” is at the beginning of the last line which is not ideal so to neaten things up a bit we changed the code in the HelloWorld.cpp file to:

Re-written code for hello World program shown in text file

To explain the changes:

  • system(“clear”); will clear the terminal screen
  • \n will force anything immediately after Hello World to be on a new line.

Then we ran the same terminal commands as before and the result was:

Commands and new results shown in command line terminal

Much neater!

A quick look at Binary Executables

As you can see from the example above the C++ code is human readable but what about that binary executable? If opened in a text editor we’re faced with a lot of gobbledegook!

This is because the compiler has translated the source code initially into object code and ultimately into a binary file that links with the library or libraries referenced in the source code (remember the #include <iostream> line?). This binary file is a machine readable format, the instructions from our source code are now in machine code which can be understood by the CPU (Central Processing Unit, or more commonly just called processor). The intricacies of processor level programming are for a future blog, but suffice it to say here that a processor carries out instructions, how it does so is governed by the set of rules or ‘architecture’ that it has been designed with.

Binary files are structured in a particular way, the most common structure being ELF (Executable and Linkable Format) because it is designed to be cross-platform (can be used on many operating systems and hardware platforms).

So, is our ‘test’ file an ELF file?

We can use the terminal to find out with the command:
file test
the first few words of the results show:
test: ELF 64-bit
which means that our 'test' file is indeed an ELF file.

Now we can delve even deeper!

We can open the ELF file 'test' with the command:
readelf -a test
-a is the option to read and display all of the information in the file, there are other options to read sections separately.

OK, so the information displayed is still not necessarily more understandable but it is much more readable at least! Utilities such as readelf can be very useful for debugging purposes, that is checking, detecting and correcting errors (bugs) in a program.

Further Information

We hope our brief introduction and demonstration of programming has been of interest and that you will be intrigued enough to try it for yourself and add your own cheery ‘Hello World’ !

External Websites

There are countless websites offering help and advice on the numerous programming languages available; and many of them offer written and video tutorials.

Whilst we endeavour to ensure that any references to external websites are accurate and relevant to the subject discussed in our blog entry, they are only provided for the purpose of further information the reader may wish to explore.
Their presence is not meant to imply that COOL Science endorses or guarantees either the information found therein, or the organisations, companies or persons who own those websites.
We can take no responsibility for the maintenance or contents of external websites.

All of the content in these pages is the intellectual property of COOL Science
Please read our Copyright Notice for more details