The Structure and Syntax of C++ Code

C++ is an amazing programming language to learn. If your dream is to become a games designer, or work at the cutting edge of science or engineering technology, then being able to code in C++ is a must. Remember, you’re never too old to learn how to code.

As you learn the basics of programming, you will begin to understand the structure of a program. The commands may be different, but you will start to see how the code works.


The structure of a C++ program can look complex, but once you get familiar with it you’ll begin to see how it flows. Every C++ code begins with a directive, #include <>. The directive instructs the pre-processor to include a section of the standard C++ code. For example: #include <iostream> includes the iostream header to support input/output operations.

int main()

int main() initiates the declaration of a function, which is a group of code statements under the name ‘main’. All C++ code begins at the main function, regardless of where it lies within the main body of the code.


The open brace is something that you may not have come across before, especially if you’re used to other coding languages. The open brace indicates the beginning of the main function, and contains all the code belonging to that function.


Lines that begin with a double slash are comments. This means they won’t be executed in the code and are ignored by the compiler. Why are they there? Comments are designed to help you, or another programmer looking at your code, explain what’s going on. There are two types of comment: /* covers multiple line comments, // a single line.


In C++, STD means Standard. It’s a part of the Standard Namespace in C++, which covers a number of different statements and commands. You can leave the std part out of a code, but it must be declared at the start with: using namespace std.


In this example we’re using cout, which is a part of the Standard Namespace – hence why it’s there, as you’re asking C++ to use it from that particular namespace. Cout means Character OUTput, which displays, or prints, something to the screen. If we leave std:: out we have to declare it at the start of the code; as mentioned previously.


The two chevrons used here are insertion operators. This means that, whatever follows, the chevrons are to be inserted into the std::cout statement. In this case, they are the words ‘Hello World’, which are to be displayed on the screen when you compile and execute the code.


Leading on, (“Hello World!”) is the part that we want to appear on the screen when the code is executed. You can enter whatever you like, as long as it’s inside the quotation marks. The brackets aren’t needed, but some compilers insist on them. The \n part indicates a new line is to be inserted.

; and }

Finally you will notice that lines within a function code block (except comments) end with a semicolon. This marks the end of the statement, and all statements in C++ must have one at the end or the compiler will fail to build the code. The very last line has the closing brace to indicate the end of the main function.

David Hayward

David has spent most of his life tinkering with technology, from the ZX Spectrum, getting his hands on a Fujitsu VPP5000/100 supercomputer, and coding on an overheating Raspberry Pi. He's written for the likes of Micro Mart, Den of Geek, and countless retro sites and publications, covering reviews, creating code and bench testing the latest tech. He also has a huge collection of cables.

Related Articles

Back to top button