Beginning C++: The Language, Compiler, Code Structure

Home / Beginning C++: The Language, Compiler, Code Structure

The Language of Ancient, C++

C++, one of old programming language that still exist today. C++ is popular and still evolving. The language is created by Bjarne Stroustrup.

The language itself is improvement of C language, language which is well known as a robust and reliable language for low level. C++ in earlier stage is called as C with class. It’s power relies on Object Oriented while it is still powerful to do stuff in procedural paradigm. Yet, C++ is still considerable as fast programming language compared to many programming language.

C++ is categorized as middle level language. It means it is good for low level interaction while high level interaction (with human) is not bad too.

Compiling Process

Simply when we create a program with C++, actually we are writing a source code in human readable form. But machine can’t understand what we write. A third party program is needed to translate what we thought in the form of machine can understand, a machine language. The work is done by a program type called compiler. Here in our case, a C++ is present to compile our C++ source code to an executable program.

A compilation process is divided into some phases:

  1. Preprocessing: An earlier stage. Compiler will read the source code and evaluate any preprocessor part which is marked by # symbol. Preprocessor examples are: #include, #define, #ifndef, #ifdef, #else, #endif, etc.
  2. Compiling: In this stage, a compiler translate our source code to assembly code. Assembly code can be thought as a source code for lower level programming. This is a third media used in compilation process and the result would be used for later stage.
  3. Assembling: The assembly source code produced on earlier stage would be translated to actual object code. In this stage, we would have one or some object code file(s) but still can’t be executed directly. If we link to other library, the object code would have a signature to that library.
  4. Linking: This is the last stage. One or some object code produced on previous stage would be linked into one complete executable program. In this stage, we get the true application we need.

More into Compilation: the commands

The common compiler used today is gcc by GNU and Visual Studio by Microsoft. But in this (and proably any articles) we will use gcc as it is open source and free of charge. In Windows you can grab TDM-GCC from Twin Dragon Media while in Linux you can update your machine with gcc and g++ frontend.

To compile a single file, simply invoke:

g++ -o programname sourcefile.cpp<br>

Where programname is the name of program which we will produce in our compilation. The sourcefile.cpp is the name of our source code. A compilation with multiple file is similiar. We can do it in one line command:

g++ -o programname sourcefile1.cpp sourcefile2.cpp<br>

Or we can do it separately and link it later

g++ -c sourcefile1.cpp
g++ -c sourcefile2.cpp
g++ -o programname sourcefile1.o sourcefile2.o

The Code Structure

C++ is structured. It has a basic layout for any C++ program. Let’s see a simple source code below and observe it:

#include <iostream>
using namespace std;

/* main function. Our program start from this point */
int main() {
	cout<<"Hello world"<<endl;
	return 0;

How is it?

Compile by yourself, you should have been able to do it. Now check the result.

Let’s inspect the code carefully:

  1. First line we have #include <iostream> and using namespace std; in second line. A line with # is a preprocessor. At this case #include will tell compiler to search a file named iostream and load it, include it with our program. This is standard library for input-output in C++. The using namespace std; tell us to use namespace std; It is a line so that we don’t need to write any member of std namespace like std::cout in complete form and only write the actual code only like cout. The namespace would be cover in later chapter so bear with it.
  2. Fifth (5th) line: we have int main(), a function. This is call entry point as all C++ program begin its operational from this point. C++ will execute code inside main() function first, sequentially.
  3. Sixth (6th) line: we have cout<<“Hello world”<<endl; This code instruct program to write “Hello world” to standard output, in this case is your monitor. You spot cout right? It is defined inside file iostream inside namespace std. Normally we should write std::cout whenever we want to call it. But as we have using namespace std, we can omit std’s and call only cout.
  4. Seventh (7th) line: we have return 0; Every function will return a value. This line express a final result of our main() function. Return 0 indicates that our program is executed normally.


About Author

about author


A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social Share Buttons and Icons powered by Ultimatelysocial