C++ - Chapter 1 - Your First Program


C++ is the most widely used programming language in the tech industry. It is used by everyone from software developers in unhealthily small cubicles, to scientists in sweaty-smelling labs, to script kiddies in their parent's dark basements.

Programming (no matter what anyone else tells you) is more art than science. There is no textbook technique that covers every programming situation you might come across. Just as many people may have similar ideas, many people have similar programming styles. Experiment with different styles (where you aren't limited to a company procedure or coding standard) until you find a fit for your personal preferences.

Definitions (in layman's terms)

First let's cover a few definitions you will need to know in order to advance your knowledge of programming.

Source Code: This is the programming code that you type into a text editor or development environment. This includes variables, comments, statements, etc.

High Level Language: A set of statements that resemble human speech. For example: "return 0;". C++ is an example of a high level language.

Machine Code: The binary string (made up of 1's and 0's) that is interpreted by the processor as a set of instructions or commands. This is the product of compiling a high level or assembly level source code.

Compiler: A piece of software that converts a high level language into machine code. Essentially this turns your source code into an executable program.

#include <iostream>

using namespace std;

int main (void)
    cout<<"Hello World!"<<endl<<endl;

    return 0;

#include <iostream> is called a preprocessor directive. There is a library file on your harddrive called "iostream" that contains the instruction set that the processor is supposed to use when certain C++ keywords are used like "cout".

using namespace std; reveals to the compiler the method calls to use for certain events, like "cout".

int main (void) is the entry point of the program (also called the "main" function). This means that when you execute the application, the first statement inside the curly-brackets is the first operation of the program. Note the int preceding main, this implies the "main" function must return an integer value as you will later see. (We'll discuss data types in a later chapter). All C++ programs (unless you are programming for a specific API) will have an int main () line.

cout<<"Hello World!"<<endl<<endl; is the called a statement. cout is the syntax used to write output to the screen (standard output). The less-than symbols are redirection operators that pipe the literal string to standard output. "Hello World!" is a literal string. This means whatever is inside the quotes gets used, regardless of the content. In this case, the string "Hello World!" is output exactly as it was entered into the code. endl is a C++ syntax keyword meaning end-line. This outputs a carriage return (newline) into the desired output by use of the redirection operators.

return 0; is the function return statement. This is the official end to every function. All functions must have a return line. Remember how it was stated previously that the main function must return an integer value? This is why the zero follows the return operation. If you wanted the program to return a 1 upon a successful exit, you would have used return 1; . Likewise for 100, return 100;, or for -1, return -1;

Note that the entire contents are positioned inside of a set of curly-brackets. Curly-brackets denote the function boundaries. Every function will have paired curly-brackets.

Index Next - Variables >