recent

Please accept my apologies for the mess — the site is under construction.

previous

about / contact

the site
the webmaster

full cv (pdf)


github
form
linkedin
youtube


writing

the Lorentz boost (coming soon)







hello world program (c++)
19:46 2/19/2012

In computer programming, the general starting point when learning a new language is writing a ‘Hello World’ program. At its simplest, such a program opens, shows the user a few words (this is called ‘printing to the screen’) generally along the lines of ‘Hello World’, and then closes. Whether you consider this to be an incredibly difficult or an incredibly easy feat (or anywhere in between), I feel that it is worthwhile to examine exactly how the program works—if only to provide the green coder with a brief introduction to C++.

When run, the program should produce something looking like this:



The code for the program is given below:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
     cout << "Hello World!\n";
     cout << "I'm a C++ Program!\n";
     cout << "Welcome to the wonderful world of programming...";
     cin.ignore();
     return 0;
}

As is normal with C++, this code opens with the "include" section which tells the complier what headers/libraries to include. Think of these headers/libraries as types of dictionaries. The computer does not understand what "cout" means by itself; however, it can be defined in such a way that the computer understands it. Rather than typing out this definition every time it is used, it is included in the "iostream" library, which can be referenced whenever the programmer wants to use a certain function. In this program, I call the complier to load the "iostream" and "stdlib.h" headers. There are many, many libraries that you can reference, but only several are ever used in simple code. The "using namespace std;" is another code-simplifier that's a bit complicated to explain without going into the various function-name-ambiguities, but essentially it makes the code easier to work with at this level.

With the exception of "int main()", we find the rest of the code within a set of braces (braces is the US term; they are also known as curly or flower brackets). Essentially, this is the part of the code that "runs". It starts at the top of what is in the braces, then performs the task each line asks it to perform.

°  The term cout << tells the computer to write whatever follows onto the screen.
°  \n is the equivalent of hitting Enter (pressing the return key). It is the same as << endl << or "end-line".
°  cin >> (not used here) is the opposite of the above cout in that it accepts user input rather than outputting to the user. The cin.ignore() function accepts user input (after waiting) and then ignores it – which is incredibly useful if you want to keep a program running until the user wants it to close.
°  return 0; tells the complier what value to return to the function. In this case, we are dealing with an integer function ( "int" main ), and returning a "0" effectively ends it.

If you hadn't guessed by now, the semicolon (that's a ; ) is used to indicate to the complier that a line of code is over. It is used after almost every single line in a program.

download
"Hello World" for Windows here. (475105bytes | 463KB) [uploaded 19 Feb 2012]

This program was virus-free at the time of upload; however, is provided as-is. If you have any qualms, download a complier and build it yourself.


This website is run and maintained by George Wong. This webpage was last modified 19:46 2/19/2012 est.
You opened this page on
www.somewhereville.com/gnw and its subsidiaries are copyright 2003-2014.