The traditional first program

In this tutorial, I’ll show what is the traditional first program taught in most introductory programming courses (and programming textbooks). If you want to follow along using windows you can obtain gcc for Windows which is the GNU C/C++ Compiler ported to MS Windows.

This is the output from a terminal session from linux:

[ron@desktop Hello]$ cat hello.c
#include <stdio .h>

int main()
printf(“Hello World!\n”);
return 0;
[ron@desktop Hello]$ gcc -ohello hello.c
[ron@desktop Hello]$ ./hello
Hello World!
[ron@desktop Hello]$

The first line begins with my linux prompt.
‘ron@desktop’ means that I’m logged in as ron on a computer called desktop (I also have a laptop).

‘Hello’ is the name of the folder that is my current working folder.

The command I entered was ‘cat hello.c’. This means display the file ‘hello.c’ to the screen. In windows, the equivalent command is ‘type hello.c’.

Using the text editor, create a file with all the lines that you see between the first and second linux prompt.

The next command I typed was ‘gcc -ohello hello.c’. This tells the gcc compiler to compile ‘hello.c’ and name the output file (-o) ‘hello’ (in windows use -ohello.exe). By default the output from the compiler is called ‘a.out’.

The final command was ‘./hello’. I told the computer to run ‘hello’ in the current folder. In future entries, I will skip the command line part of creating the program and just show source code.

In hello.c the parts of the program are as follows

#include is a compiler directive. There are a number of compiler directives. They all give the compiler directions to do something. #include tells the compiler to include another file in this one (as though it were part of this program) before compiling.

<stdio .h> is a header file that is installed with the compiler. It includes definitions for standard input/output functionality that is also installed with the compiler.

int main() defines the place where the program will begin execution. ‘main()’ tells the compiler where you want the program to start. ‘int’ tells the compiler that the program is going to provide the operating system with an integer value indicating the status of the program when it ended (0 means exited normally, success, or OK).

{} tells the compiler where ‘main()’ begins and ends. What is between the 2 braces is called a block of code.

printf() stands for print formatted. It is one of the functions that is defined in stdio.h and included in the C libraries.

“Hello World!\n” is called a string literal. The ‘\n’ is the newline character.

; terminates the line of code. Most lines of code in C programs end with a ;
So, printf(“Hello World!\n”); tell the computer to print Hello World! to the screen and go to a new line.

return 0; tells the computer to end the program and return a value of 0 to the operating system (telling it that the program was successful.

And there you have it. You’ve written a computer program.

Author: Ron

Homeschooling dad of 4 (ages 27 - 14), grampy to 3, WordPress core contributor, former farmboy & software developer by profession.