C++ Syntax: What is the Basic Syntax of C++?

What is syntax in C++?

The term “syntax” refers to the predetermined collection of guidelines, procedures, and standards that must be followed to produce error-free code. Even the C++ programming language has its unique syntax.

Additionally, the C++ program is viewed as a group of objects interacting by executing each other’s methods. Its syntax comprises classes, methods, objects, and instance variables.

C++ Program Structure

The structure of a C++ program is divided into several sections.

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}

This example code prints a "Hello World!" output, and here’s the explanation of how the code works:

#include <iostream> – It is a library of header files that allows us to interact with input and output objects. It is used to receive input with “cin” and produce output with “cout“.

using namespace std; – is known as a standard namespace, which means we can use the names of objects and variables from the standard library without prefixing them with std::.

int main () -is a function, and it always appears in every C++ program. This function directs the program to execute any code inside the curly brackets { }.

The cout is a class object in ostream that is read as “c-out” wherein “c” stands for character and “out” means output. It is an object that displays the output “Hello World” along with the operator <<.

Always remember that every statement in a C++ program must end with a semicolon (;). The C++ compiler also ignores white spaces. However, this makes your program more readable and organized.

return 0; – means to return a value or stop a function’s execution. And to properly end the main() function, do not forget to put the closing curly bracket }.

Compile and Execute C++ Program

To compile and execute the C++ program, you will need to follow a few steps. These steps will guide you through saving, compiling, and running the program.

Here are the steps for compiling and running our C++ program.

  • Step 1: Open an applicable text editor for C++ codes and paste the given example above.
  • Step 2: Save this example as a .cpp file and note its directory.
  • Step 3: If you used a complete compiler, such as VS Code, you could directly compile and run your program.
    If not, you can run your program using Command Prompt. Using cmd, find the path where you saved the .cpp file and type “g++ name.cpp” and click enter.
  • Step 4: The command prompt will advance to the next line and create an a.out executable file where you can execute the program.
  • Step 5: The command prompt will execute the program and display the output “Hello World”.

If you want to learn how to set up a C++ Environment, you can visit our C++ Environment Setup Tutorial.

Semicolons and Blocks in C++

Semicolons in C++ programming are commands that terminate (end) a statement. It informs the compiler that the function has completed a command. Typically, a semicolon divides one piece of C++ source code from its associated code. This indicates that the separation is purposeful.

We use semicolons for the following statements:

a = b;
b = b * 2;
add (a, b);

Blocks is an entity in C++ programming. It consists of logically connected programming statements inside the opening and closing braces. Take a look at this example:

{
  cout << "Hello World!";
  return 0;
}

C++ Identifiers

An identifier in C++ is a name used to identify a variable, function, class, module, or other user-defined objects. We can use A to Z, a to z, an underscore (_), zero or more letters underscored, and digits (0 to 9).

However, identifiers in C++ syntax cannot contain characters such as @, $, or %. C++ is a case-sensitive programming language; therefore, ABC and abc are distinct identifiers. Have a good look at the example C++ Identifiers:

NAME
name
name_123 
Name_name
a1b2c3
naMe

C++ Keywords

C++ keywords are words that cannot be used as an identifier. These are reserved words because they have special meaning in C++ compilers and are always written lowercase.

Let’s look at some C++ Keywords examples to avoid using them as identifiers.

alignasdecltypenamespacestruct
alignofdefaultnewswitch
anddeletenoexcepttemplate
and_eqdonotthis
asmdoublenot_eqthread_local
autodynamic_castnullptrthrow
bitandelseoperatortrue
bitorenumortry
boolexplicitor_eqtypedef
breakexportprivatetypeid
caseexternprotectedtypename
catchfalsepublicunion
charfloatregisterunsigned
char16_tforreinterpret_castusing
char32_tfriendreturnvirtual
classgotoshortvoid
complifsignedvolatile
constinlinesizeofwchar_t
constexprintstaticwhile
const_castlongstatic_assertxor
continuemutablestatic_castxor_eq
List of C++ Keywords

Trigraphs

A trigraph in C++ is a series of three characters representing a single character. This indicates that trigraph sequences consist of three characters beginning with two question marks (??).

Trigraphs are enlarged wherever they appear, including in string literals, character literals, comments, and preprocessor directives. Examples of Trigraphs are as follows:

TrigraphEquivalent
??=#
??/\
??’^
??([
??)]
??!|
??<{
??>}
??-~
List of Trigraphs Examples

Whitespace in C++

A line having simply whitespace is referred to as a blank line, and the C++ compiler completely disregards it.

In C++, whitespace is the name for blanks, tabs, newline characters, and comments. Whitespace divides one section of a statement from another. This allows the compiler to determine where one element (int) stops and the next one begins.

Take a look at these Examples:

int name;

There must be at least one whitespace character between int and name to distinguish the function (int) and the variable (name).

female = gender - male; // Count all female

In this line of code, the compiler disregards the whitespace between the variable (female) and the operator (=). However, you are free to use whitespace characters to make your codes readable.

Summary

In summary, this tutorial discussed the basic syntax of the C++ language, the knowledge about the parts of its syntax, and their meaning.

The C++ Syntax is pre-defined and is set as rules implemented in compilers to compile and execute C++ codes properly. It is the programmer’s guideline in creating a creative and error-free program. Therefore, the C++ syntax governs how the programmers write, compile, and run the C++ program.

C++ programming has a lot of potentials and is utilized by many in this generation. This language provides a wide range of opportunities, and to start yours, you must prep yourself by knowing the C++ Basic Syntax.

Leave a Comment