Lesson 1 - Hello World
The first thing you're going to need to learn to create a program in C/C++ is your basic "Hello World!" application. This will print the words "Hello World!" at the command line and exit.
Setting Up a Workspace
First, we're going to want to create a folder in your Home Folder to hold this project.
Double click on the Home icon on your desktop, then from the menu bar choose File -> New Folder. Create a folder named code and then open it. Now create a folder named lesson-01.
Next, open the Terminal. In the terminal you can use the command pwd to see which folder you're currently in. By default, you'll always start in your home folder.
Use the command cd to change directory. First, type cd code to go into the code directory, then cd lesson-01.
Creating a New File
From Terminal you can very quickly create a new empty text file by using the touch command. Type touch main.cpp to create a new file.
Now, double-click that file in Tracker to open it. It should automatically open in the code editor application Pe.
Writing the Program
First you'll need to inculde a file named stdio.h - this is the standard input/output library. This will give us the functions we need to output to the command line
#include <stdio.h>
Next, we need to create what's called the entry point. C and C++ programs don't start at the top of the first file - they start in a function named main.
#include <stdio.h>
int main() {
}
The main function is defined as an integer function, meaning that it returns a value at the end. We'll cover this more when we get to functions but for now you'll need to make sure that you add the following line:
#include <stdio.h>
int main() {
return 0;
}
Now, we need to add the line that will output text to the terminal. We're going to use the C function printf (print formatted). Note the "\n" at the end of this text - that tells the terminal to put a line break at the end.
#include <stdio.h>
int main() {
printf("Hello World!\n");
return 0;
}
Also note: C/C++ statements end in semi-colons. You can break things up into multiple lines if it makes it easier to read. The C/C++ compiler will ignore those line breaks until it hits a semi-colon.
Click the disk icon to save your file.
Compiling Your Program
Return to the Terminal window and type the following:
g++ -o lesson-01 main.cpp
This will compile main.cpp and output a file named lesson-01. Now you can run your program by typing the following:
./lesson-01
Congratulations, you've just completed your first C/C++ program!
Using C++ Style Output
C++ has some slightly different output methods. To use C++ output methods, change the code to the following:
#include <iostream> // include iostream instead of stdio.h
using namespace std; // tell C++ to use the standard namespace
int main() {
cout << "Hello World" << endl;
return 0;
}
The results should be the same.
C++ output tends to be pretty clunky so I prefer to use the C input/output methods when I can. There are benefits to the C++ style, but they really aren't going to factor in to a beginner-level tutorial series like this.