If you’ve always wanted to dabble in C, but never quite gotten around to it, have a read through this and give it a go.
It is much easier than you might think (easier now, in fact, than it ever has been), to write your first C program, and there is something so deliciously inviting about the language that once you get started you’ll soon be solving world issues with your code.
Have you seen my C++ Eclipse Masterclass
It’s a series of professional video and text tutorials on using C++ and Eclipse CDT.
This is a tutorial on creating and understanding a ‘Hello World’ program in C++ using Eclipse.
Why? Because in the majority of my other C++ tutorials I will be using Eclipse as the IDE (integrated development environment), and building on a basic project such as this.
I program on Fedora Linux, not Windows, so although your screenshots may differ slightly to mine, Eclipse runs quite happily on both.
There’s a wealth of information on the Eclipse website that tells you how to install Eclipse for your operating system, but the heads up if you’re a Fedora user is just switch to root (or use sudo) and run:
yum install eclipse-cdt
Create your project
Open eclipse (you’ll find it in the Applications > Programming menu), and if this is the first time you have used it, accept the default workspace and close the welcome window.
Then go to:
Window > Open Perspective > Other > C/C++
This opens the C++ ‘perspective’ in Eclipse, which is a fancy way of saying it makes available the windows and options you need to develop C++ programs.
Now create your first program with:
File > New > C++ Project
Name it Hello, and select the project type as Hello World C++ Project (Figure 1).
Click next and accept all the defaults in the Basic Settings window. Click next and again accept all the defaults in the Select Configurations window. Click Finish.
Eclipse will create your project and project file and return you to the main C++ view. You should now be able to see your ‘Hello’ project on the left hand side of the screen, and the contents of the Hello.cpp file in the main window. If you expand the project folder on the left, and then expand the ‘src’ folder underneath it, you will see the location of your program file, Hello.cpp (Figure 2).
Your project in action
Now all you have to do is hit ‘Run’ (it’s that little green button that looks like a play symbol).
Note: Eclipse may be set to NOT build automatically, in which case you will get an error about the binary not being found. If this happens, select Build (the hammer icon) before trying to Run.
You should see “!!!Hello World!!!” in the console area at the bottom of the screen (Figure 3). Note that if you prefer a pop-up console window, you can right click and select the ‘Detached’ option on the console tab. Even if you close the window, it will reappear next time you run the program.
That’s great, but what does it all mean?
Let’s just run through the contents of the Hello.cpp file, so you have an idea of what you have created.
The first 7 lines are just comments. They were created for you by Eclipse and you can edit these to your heart’s content. What’s included at the top of a cpp file varies widely between companies and also depends on personal preference, but bear in mind that the point of the comments is to tell the next person that comes along a little bit about what this file contains.
The first two lines of actual code are:
using namespace std;
Any line that starts with a # character is called a ‘preprocessor directive’. The preprocessor makes any requested modifications to your source code before passing it to the compiler. In this example, line 9 is telling the preprocessor to include something called ‘iostream’, which refers to a header file that allows our program to utilize the input/output methods in the iostream library.
I will talk a little more about the preprocessor in a future tuturial, but for now you can look at this line as a succinct way to include pre-existing code.
Line 10 makes use of the using directive to tell the compiler that we are using functions from the standard namespace.
OK, a namespace is basically a collection of classes and functions grouped together under an umbrella and given a name. In this case the name is std, and it covers all the things you might want to use from the C++ standard library. Namespaces help resolve the potential issue of classes and functions having the same name and thus causing redefinition errors when you try to compile your program.
In our example, the functions we are using from the standard library are cout and endl. If you didn’t have line 10 in your source file, you wouldn’t be able to use these, because they would not be in scope, or in other words, the compiler would not know what on earth you were referring to*.
This is the most important part of the file – it’s referred to as the main function and is where our program starts and ends. main is defined only once for any C++ (or C) program, and the name is always all lower-case.
It contains two statements inside a set of curly brackets. The first uses the function cout to print the ‘Hello World’ text to the screen (or standard output), and finishes with endl, to declare an end of line character (the equivalent of pressing the return key after typing in a sentence).
The second statement returns a zero when the program exits. Traditionally a zero return means that the program has executed without errors. It is required here because the main function return type is declared as int (short for integer), meaning a whole number of some value must be returned when the program exits.
Tell me what I’ve learnt
Well, that just about covers it. Of course, you can dig into the details of just these few lines of code and read a huge amount on the hows and whys of it all, but to summarize what this basic tutorial covers, you should have done the following:
1) Created a Hello World C++ program in Eclipse.
2) Run that program and seen the output in the console window.
3) Identified the different parts of the program: comments, preprocessor directive and code.
4) Understood the importance of the main function being our program’s entry and exit point.
5) Noted the use of the std namespace to allow us access to the functions cout and endl.
6) Appreciated the use of these functions to output a text message to the screen.
Phew! That’s enough for today. Look out for the next C++ tutorial, coming soon.
*An alternative to the using directive at line 10 is to qualify each std method with its namespace when it appears in the code, i.e.:
std::cout << "!!!Hello World!!!" << std::endl;
This is considered a better way of doing things, but typing std:: over and over can be a little laborious, hence the widespread use of the using directive on line 10. If you are after more detail, there is a good explanation of this at the C++ FAQ here.