Program Structure


Here is an example C++ code and its algorithmically equivalent code in MATLAB. The first thing to note is the data type identifiers before the variables. In C/C++, variables must be declared. Their type and size must be specified. While in MATLAB a variable is an object by default.

#include <iostream> 

int main(int argc, char* argv[]) { /* The main function is the starting point of any C++ program */
    int a = 120; /* Variables must be declared, their type must be specificied */
    int b = 250;
    int c = a + b;
    /*In C++ std::cout is a way to print content to the output terminal */
    std::cout << "The sum of " << a <<" and " << b << " is equal to "<< c << std::endl;
    
    return 0; /* main is a function that returns a variable */
}
Output:
The sum of 120 and 250 is equal to 370

Similar MATLAB Program Structure

a = 120;
b = 350;
c = a + b;
disp("The sum of " + a + " and " + b + " is equal to " + c)

In MATLAB we use the semicolon to disable output to the terminal. While in C/C++ a semicolon is required to denote a line end.

In C/C++, there must be at least one function, called the main() function. This is the function that gets called first when the program executes. Think of it as the first block in a board game. Since main is a function, it can take in variables and can return a variable as well.

Snake Boardgame

To print out words and numbers on the terminal we use the cout stream operator, which is part of the standard library, and more specifically available as part of the <iostream> header. To denote a line end in C++ you can use the endl stream operator, or "\n" (a new line character, more on that later)

Not including any headers, also referred to as libraries, we are left with the bare minimum C/C++ functionality. Which is very limited. If you are coming from MATLAB, you need to be aware of this difference. In MATLAB there are lot of built-in functions and preloaded libraries (MATLAB takes time to load right?) that don’t require explicit include.

Even mathematical functions such as trigs, power and square are not available in bare C/C++. But if you are coming from MATLAB, they exist out of the box. For math functions for instance, there is the <cmath> header file.

File Structure


Let’s familiarize ourselves with the files associated with a C/C++ project. There is not a single way these files are organized in, but here is one common folder structure.

Under the folder commonly abbreviated src, which stands for source, you will find, with no great surprise, source files. Those are the C++ *.cpp files or C *.c files. You can have a running C/C++ program just by using source files.

Under the folder abbreviated inc, which stands for include, you will find the header files. And those are the files with the *.hpp or *.h extension, for C++ and C respectively. This is the project include folder, there may be other include folders pointing to external “libraries”. You can have as many include and source folders as you specify.

Under the folder lib, which stands for library, you will find the library files. The files with *.a and *.so extensions. These are pre-compiled code. Perhaps they come from a third-party library or a different or old part of a big project.

And then there is the bin folder, which stands for binary, this is where the final application or executable resides. You will probably come across many different folder structures for a C/C++ program, all that is important, is that the compiler knows where they are. You can place all relevant files in a single folder even (messy, but still possible).

As a beginner, you need to familiarize yourself with the source and include files and their locations. The executable files are generated by the compiler once you build your program, so it’s not something you need to worry about as a beginner.

File Explorer View

Next: Data Types