c-basics-keywords-identifiers-blocks-delimiters

C++ Basics: Keywords, Identifiers, Blocks & Delimiters

Quickly Learn All The Basic Syntax Of C++ With Examples.

In this In-Depth C++ Training Series, we will learn about the basic syntax of C++ including keywords, identifiers, blocks, and delimiters in this tutorial.

These terms act as a foundation for C++ programming and we need to know them thoroughly before we begin to start actual programming in C++ language.

What You Will Learn: [hide]

  • Delimiters And Blocks In C++
  • Identifiers In C++
  • Keywords In C++
  • Comments In C++
    • Single-Line Comments
    • Multi-line Comments
  • Conclusion
  • Recommended Reading

Delimiters And Blocks In C++

In C++, the end of a logical statement or entity is denoted by the use of statement delimiter or terminator. The statement terminator used in C++ is a semicolon (;). Every C++ statement has to end with a semicolon.

For Example, see the following statements,

x = 1;
a = a +1;

As shown above, each statement has ended with a semicolon is a separate statement.

Another entity in C++ is “blocks”. A block is a set of logically connected programming statements enclosed with opening and closing braces.

Following is a valid block in C++:

{
                   int i=1;
                   cout<<”This is a C++ block”;
                   i++;
                   cout<<”value of i = “<<i;
}

As shown above, block in C++ can even have variable declarations, definitions, and any other valid programming statements.

One thing to note here is that C++ does not recognize the end of the line as the statement terminator.

For this reason, the following statements,

a=1;
a = a+2;
cout<<a;

Can be written on one line as follows:

a=1; a = a+2; cout<<a;

Hence both the ways of writing the statements shown above are valid statements.

Identifiers In C++

Every entity in C++ such as a variable, functions, class, module, etc. needs a name to identify it. This name that we give to entities is called an “identifier”. An identifier in C++ usually starts with a letter from A to Z or a to z or an underscore (_) followed by zero or more alphabets, numbers or underscores.

Following are some examples of valid identifiers:

abc _one myarray test123

C++ doesn’t allow having identifier names to start with numbers and also it doesn’t allow identifiers to include characters in its name other than an underscore.

So the following identifiers are all invalid identifiers.

8num num@array my#name @email

As C++ is a case-sensitive language, the same identifier names but with different cases will be two different identifiers. Thus the two identifiers myarray and Myarray are different.

Keywords In C++

Keywords are specific words which are reserved by the compiler for its own use. These are mainly words like some in-built functions, data types of variables, filenames, operator names, etc. which we will use in our program.

As the keywords are all reserved by the compiler, we cannot use these words while naming variables, constants or any other identifiers.

C++ has numerous keywords and we have listed them in the following table:

asmelsebreakregisterreturnsigned
newthisprotectedtypeidunionusing
autoboolcasecharconstcontinue
enumexplicitexternfloatfriendif
operatorprivatepublicreinterpret_castshortsizeof
throwtruetypedeftypenameunsignedvirtual
breakexportcatchclassconst_castdefault
protectedtryfalseforgotoinline
staticvoiddeleteintstatic_castvolatile
dolongstructwchar_tdoublemutable
switchwhiledynamic_castnamespacetemplate

Comments In C++

Comments in C++ are informative statements that are used in C++ program to explain the various constructs, program logic or functions used in the program. The comments help to make the program more readable and understand the code more clearly. Whenever a compiler encounters a comment, it ignores it completely.

C++ supports two types of comments i.e. single-line comments and multi-line comments.

Single-Line Comments

Single line comments are basically written in one line only and they begin with characters “//” and extend till the end of the line. Thus when the compiler encounters “//”, it recognizes the beginning of the comment and ignores everything that follows it.

Following are few single-line comments in C++:

//this is a single-line comment

//I am in the main function

Multi-line Comments

Multi-line comments or block comments are the comments enclosed between “/*” and “*/”. Everything inside the block that starts with “/*” and ends with “*/” is ignored by the compiler. These type of comments are not necessarily one-liner but they can extend to multiple lines.

Example of Multi-line comments is given below:

/* This is an example of
multi-line comment
in C++ programming language */

Conclusion

With this, we have concealed basically about the fundamental terms that we use in C++ programming.

Related Posts