C++ Program Structure & Syntax
In this tutorial, we’ll take a quick look at a few topics that are critical to any C++ programme. Because there are so many topics to cover, we’ll cover the majority of them at a high level. This chapter’s goal is to teach you how to build simple C++ programs. You’ll be able to write your own simple programs by the end of this chapter.
The majority of these topics will be revisited and discussed in greater depth in subsequent chapters. We’ll also introduce new ideas that will build on these.
C++ Basic Syntax
Consider the following code, which prints the words Hello World.
#include <iostream> using namespace std; int main() { cout << "Hello World"; return 0; }
Lets look at each line of the program
- Line 1 is a preprocessor directive, which is a special type of line. This preprocessor directive indicates that we want to use the contents of the iostream library, which is a component of the C++ standard library that allows us to read and write text from/to the console.
- The compiler is instructed to utilise the std namespace by the line using namespace std;. C++ has only recently added namespaces.
- The following line is a single-line comment in C++: ‘/ main() is where programme execution begins.’ / starts a single-line comment and ends it.
- The main function, int main(), is where the programme starts to execute.
- The next line, cout “Hello World”;, displays the message “Hello World” on the screen.
- The next line, return 0;, closes the main() function and returns 0 to the calling process.
There are few terminologies that you want to understand before you proceed with other lessons.
Statements
A computer programme is a set of instructions that teach the computer on how to perform certain tasks. A statement is a type of instruction that instructs the computer to carry out a specific task.
In a C++ program, statements are by far the most common sort of instruction. This is due to the fact that in the C++ language, they are the smallest independent unit of computation. They behave similarly to sentences in natural language in this way. We usually write or speak in sentences when we wish to express an idea to another person (not in random words or syllables). When we want our programme to accomplish something in C++, we usually write statements.
Semicolons
The semicolon is used as a statement terminator in C++. That is, each statement must be terminated by a semicolon. It denotes the conclusion of a logical object.
Blocks
A block is a collection of logically connected statements separated by opening and closing braces.
Functions
Statements in C++ are typically grouped into units known as functions. A function is a collection of statements that are executed in a specific order. You’ll be able to create your own functions and mix and match statements as you learn to write your own programs.
Syntax Errors
Sentences in English are built using particular grammatical rules that you most likely studied in English class at school. Normal sentences, for example, end with a period. Syntax refers to the principles that control how sentences are created in a language. It is a breach of English language syntax to omit the period and run two sentences together.
C++ has its own syntax, which specifies how your programmes must be built in order to be considered acceptable. When you compile your programme, the compiler is in charge of ensuring that it matches the C++ language’s basic syntax. If you break a rule, the compiler will protest and give you a syntax error when you try to compile your programme.
Let’s see what happens if we remove the semicolon from line 5 of the “Hello world” programme, as shown below:
#include <iostream> using namespace std; int main() { cout << "Hello world" return 0; }
You will receive an error from the C++ compiler stating that there is an issue with the syntax.
This indicates that you have a syntax error on line 6: the compiler was looking for a semicolon before the return statement but couldn’t find one. Although the compiler will tell you which line of code it was compiling when the syntax error occurred, the omission could have occurred on a previous line.
When writing a programme, syntax errors are common. Fortunately, they’re usually easy to find and fix, as the compiler will usually point you in the right direction. A program’s compilation will be completed only after all syntax errors have been resolved.