System ProgrammingLearn the basic Syntax in C++ Programming Language

Learn the basic Syntax in C++ Programming Language

After writing our first program, let’s spend some time learning the C++ syntax.

Curly Braces { }
Any code block in C/C++ must be delimited by curly braces {}. The bodies of a loop, function, if condition, structure, and class definition, all should be surrounded by curly braces. With very few exceptions, this is a general rule.

Statements are the program instructions. They can be considered as the building blocks of computer programs, i.e. a typical program consists of one or more statements. Each statement tells the computer to do something. For example, our first program consists of one statement:

printf("Hello, I am a C++ programmer");

This statement tells the computer to print the quoted string to the standard output.
C/C++ statements must be terminated with a semi-colon “;”. A line may contain more than one statement if desired.

A statement could also span multiple lines (if desired, but I advise not to do). For example, the above statement could be re-written as:

("Hello, I am a C++ programmer");

The program will compile and execute successfully without even warning (but as I told you, I don’t recommend this way).

Documenting your work is vital for success. We are humans, and forgetting is a human nature. You may write a great program of five or ten thousands of lines, but as things do change, there might be a need in future to make some modifications on this program. Whether these modifications would be made by you or by somebody else, without proper documentation, you (or the other guy) will get lost. Don’t let pride take you, I am telling you about facts, and based on a personal experience.

Comments achieve this task. They are human-readable, and meaningful text embedded inside your code. A compiler is made aware that the following text will be a comment by using a specific syntax. On compiling the source code, comments are completely ignored by the compiler.

C/C++ supports two types of comments: single-line, and multi-line comments.

Single-line Comments:
A single-line comment is marked by two forward slashes //. When encountered by the compiler, the compiler considers everything after the // and up to the end of the line as a comment.

Multi-line Comments
Multi-line comments can span multiple lines. They are delimited by the /* and */ sequences; i.e. when the compiler sees the /*, it considers everything coming after it as comment. This continues until a closing */ is encountered.

In this example, we are going to re-write our first program with some comments added.

Notice that comments are written in a different color to be easily distinguished.
Needless to tell you that this modified version will produce the same output as the first version.

Whitespaces are also ignored by the C++ compiler. So feel free to insert whitespaces (or not to do) as you prefer.

The following is a modified version of the first program with very few whitespaces (and of course with the same result as previous ones)

Empty Lines
Like whitespaces, empty lines are also ignored by the C++ compiler.

Alternate Way to Write to Standard Output
Another way to write data to the standard output is using the cout. cout is an object of the ostream class (declared in the header file iostream.h), which represents the standard output stream. The insertion operator << is used to redirect any content on its right to the object on its left (in this case: the cout)
Learn the Basics of C Programming Language

Let’s re-write our first program using the cout << method. The program could be like the following: 4

Notice that the preprocessor directive now asks the compiler to include (insert) the header file iostream.h (instead of the stdio.h that was needed for the printf function to work). Without specifying the correct header file(s), the compiler will fire error(s).

The output should be the same.

Inserting New Line
If you run the first script several times, you will notice an ugly thing in your output window:

The printed statements are all written to the same line. This tells us an important thing; printf and cout don’t insert new line by default. To print a new line, you have to specify it. (this is unlike some other languages that insert the new line character by default with some tools. e.g: echo in UNIX/Linux, and println in Java)

To insert a new line, we have two options: either using the well-known escape sequence ‘\n’, or using the endl manipulator. So, the statement could be:

cout<<"Hello, I am a C++ programmer"<<endl;


cout<<"Hello, I am a C++ programmer\n";

Both will do the job as required. Now, look at the modified program’s output after inserting the new line character:

With this modification, each execution prints the hello message in a separate line.

Escape Sequences
The escape sequences are sequences of characters that have special meanings. They are used to represent special characters like new line, tab, beep, etc. We have already seen an example of escape sequences (‘\n’ that represents the new line character). Escape sequences always start with backslash \
In future articles, we will use these escape sequences frequently.

Variable names, function names, class names are called identifiers. Rules for creating identifiers in C/C++ are:
• An identifier may contain uppercase letters, lowercase letters, numbers, and underscore.
• An identifier must start with either letter (upper or lowercase), or underscore.
• C/C++ are case-sensitive: so, counter, COUNTER, and Counter are three different identifiers for three distinct variables (or classes, or functions).
• An identifier must not be a reserved word.

* * * * * *

• In this article, we have talked about the basics of the C++ syntax.
• Code blocks are delimited by curly brackets { }.
• Statements are the building blocks of computer programs.
• C/C++ statements are terminated by semi-colons.
• Where possible, document your programs by adding comments.
• C/C++ supports two types of comments: single-line, and multi-line comments.
• Whitespaces and empty lines are ignored by the C++ compiler.
• Besides to the printf function, cout is used to write to the standard output.
• You can print a new line using either ‘\n’ or the endl manipulator.
• Escape sequences are special sequences that start with backslash, and represent special characters.

That was part three; see you in the next article: Variables.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -