Hello, world is the most basic of all test programs. It’s one of the first steps to determine if you have your development environment set up correctly and is also a good first program to learn in any language because it will give you a good idea of the bare minimum requirements to build a program in a given language.
BASIC
Let’s start with Hello, World in basic since that was the first programming language I ever encountered. The code is very simple:
10 PRINT “Hello, World”
RUN
10 is a line number which is a requirement of old basic compilers. Generally the lines are numbered 10, 20, 30, etc although they don’t have to be. Part of the reason for this is so you can later insert other line numbers into your code without renumbering all of your lines of code. Newer BASIC implementations have sub-routines and other features have have allowed them to ditch the line numbers. The program is executed from top to bottom so everything in basic has to be in order. The command RUN at the bottom runs the program and will display your text. It is not actually part of the program.
Ruby
In Ruby, there are two easy ways to do a hello world program. I’ll assume you are using a Unix-based machine to try this example. You’ll need to create a text file called hello.rb with a text editor. The first way is:
puts ‘Hello, World’
Save your file and type ruby hello.rb at the command prompt. If all went well, it should print Hello, World and then give you back your command prompt. I’m assuming that you have installed ruby and it’s in your path. On a Debian system, it’s easy to install ruby if it’s not already there. Use a command such as sudo apt-get install ruby. The other ruby hello world example is:
print(“Hello, World\n”)
C
Hello, World in C is far more complex than any of the other examples. This is because C is highly structured and not very forgiving. It also doesn’t have the printf function in the core language so the standard input and output library must be included. I will also assume you are using a Unix environment for this example. If you try to run this in a dos/windows environment it will probably fail. Here is the hello world code in C:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(“Hello World\n”);
return 0;
}
To run it, you’ll need to put the code in a text file called hello.c. Then type in gcc hello.c at the command prompt. This should generate a file called a.out. At the command prompt, type ./a.out to run the program. Different compilers and environments may give different results.
Bash
The last example I’ll give is for bash scripting. Some people would argue that bash is not a programming language but nevertheless it’s a very powerful way to accomplish tasks with other programs and it’s a good way to automate repetitive tasks. Here is hello world in bash:
#!/bin/sh
echo “Hello, World”
To run this you could simply type the bottom line at the command prompt. Instead, save it to a file called hello.sh. Now before you run it, you’ll need to set the executable bit. Type chmod 755 hello.sh at the command prompt. Now you can type ./hello.sh to execute the script.