2. Hello, World!

"Programming is learned by writing programs." -- Brian Kernighan

2.1 Programs

A program is a command which tells the computer what to do. Programming is the activity of writing and testing such programs.

Computers are dumb, in the sense that they require instructions to complete a task that a human can do with common sense. The language in which instructions are given is the programming language. C++ is the programming language for our course.

2.2 The classic first program
Hello, world!

The classic "Hello, World!" program:

                
                // This program outpts "Hello World" to the monitor
                #include <iostream>
                int main() {
                    cout <<"Hello, World!\n";
                    return 0;
                }
                
                

Let's look at the meaning of each line:

  1. // is a comment. It is ignored by the compiler, and is included to make the code more readable.
  2. #include is a directive; it instructs the compiler to include facilities from, in this case, iostream.
  3. main is the function where the computer starts executing the code.
  4. cout refers to the standard output stream, it is an abbreviaton of "character output". Characters directed to cout using << will appear on screen (by default -- that can be changed by how you invoke the program).

Every C++ program should have a main function to indicate where to start executing the program. A function has four parts:

  1. return type: specifies type of result that particular function would return to the callee: here, it is int.
  2. name: here main.
  3. Parameter list: the list of arguments the function takes, which is empty in the above example.
  4. function body: enclosed in curly braces, stating which actions the function must perform.
2.3 Compilation
A parse tree for some C code.

C++ is a compiled language which means that the code has to be translated to machine instructions before the program is run, by a compiler. (A language like Python translates to machine language as the program is run, without a separate compile step.)

Source code is the code you write: for C++ the suffix for source code files is .cpp. For the machine code or object code, the suffix is .o (Unix) or .obj (Windows)

2.4 Linking
A linker.

Several parts of code must be compiled and the resulting object files must be linked together to form an executable. The linker does this.

Object code and executables are not portable among systems i.e. object code for Windows will not run on Unix machines.

  • Compile-time errors: errors found by the compiler.
  • Link-time errors: errors found by the linker.
  • Run-time/logical errors: found when program is run.
2.5 Programming environments
The Anjuta IDE.

To program we either use the command line or an IDE. To write and run a program, we require a compiler, a linker and an editor.

  • When using the command line, you need to compile and link code by typing commands.
  • In an IDE (Interactive Development Environment) you simply click a button, which performs the compile, link, and run commands for you. IDEs also have features like color coding to help distinguish between comments, keywords and other parts of program.

Debugging is finding errors in a program and removing them. Debuggers are aids to doing this.

Test Yourself!
  1. Name the four parts of a function.
    1. return type, name, parameter list, function body
    2. main, include, link, compile
    3. cout, std_lib, class, comment
  2. What does the linker do for your program?
    1. Turns your C++ code into an object file.
    2. Pulls in all of your include files.
    3. Joins separate object files and libraries into a single executable.
    4. Handles pre-processing directives.
  3. What is the difference between a source file and an object file?
    1. The source file is in Python and the object file is in C++.
    2. The source file tells you where to look on the Internet for good material and the object file complains.
    3. The source file is text in your programming language and the object file is binary.
  4. What does an .h suffix at the end of a file signify in C++?
    1. It is a heavy file that will take up lots of hard disk space.
    2. It is a hard-coded file with lots of constants.
    3. It is a healthy file that it would be good to incorporate into your program.
    4. It is a header file containing various definitions.
  5. What function must appear in every C++ program?
    1. printf()
    2. cout
    3. main()
    4. cin
  6. In our first program, what is the purpose of return 0;?
    1. It signals to the OS that the program failed.
    2. It signals to the OS that the program succeeded.
    3. It tells the user that this program did nothing of interest.
Answers

1. a; 2. c; 3. c; 4. d; 5. c; 6. b;

Drill
  1. Make your own GitHub repository for your code: this is not optional, it is a course requirement.
  2. Login in to your PythonAnywhere account.
  3. Clone the repository: git clone <your-repo-here>
  4. Copy in the code directory from my repo.
  5. Check this code into your GitHub repo:
    git commit -a -m "First commit of course code."
    git push origin master
  6. Compile the code on PythonAnywhere:
    g++ -std=c++11 -o hello hello.cpp
  7. Run the program:
    ./hello
  8. Create different errors in the program and re-compile at least five times. See what message the compiler gives you. Talk with a classmate and see if you can understand the error messages.