Main Content

Hello World in C++ - a first program, with the process explained

Archive - Originally posted on "The Horse's Mouth" - 2015-10-30 09:23:39 - Graham Ellis

"Hello World" programs - traditionally (a good tradition) te first example on a proogramming course in any language - just output the words "Hello World" or similar text and do little else. They show learners / students / delegates the mechanism of working with a language in te simplest possible example - how to enter the code, how to turn the code into something that can be run, and how to actually run it.

Here's a "hello world" program in C++ - this text being entered into a file with an extenstion ".c++" or ".cpp" using your "favourite text editor". Use gedit, use notepad or notepad++, use emacs, nano or vi. (This text is know as the souce code)

  #include <iostream>
  using namespace std;
  int main() {
     cout << "Welcome - and enjoy your C++ course" << endl;
  }


In C++, having entered the file of text you need to convert it into an "executable program" - a file of instructions for that the processor of your computer understands rather than the English-like (!) language of C++. This process is known as "compiling" and on Unix / inux / OSX systems the easiest way is often the "make" utility:

  WomanWithCat:harwell grahamellis$ make hw
  c++ hw.cpp -o hw
  WomanWithCat:harwell grahamellis$


The resultant file - called hw in our example - can then be run as follows:

  WomanWithCat:harwell grahamellis$ ./hw
  Welcome - and enjoy your C++ course
  WomanWithCat:harwell grahamellis$


An exercise such as this may appear trivial, but it lets delegates who are using our systems (or who are new to the tools on thei own systems) get a first bite at using the elements and pasting them together - and those who are used to scripting or pseudo-scripting languages to come to terms with the need for an extra compile stage for C or C++.

Source code at [here].