C++ Basic Syntax

C++ Basic Syntax – In the previous article, you wrote your first C++ program. But if you want to write more C++ programs then you will need to learn the syntax of C++ Programming Language. In this article, we will cover C++ Basic Syntax and C++ keywords.

C++ Keywords

Any Programming Language has a list of keywords. These keywords help us for constructing C++ Basic Syntax. C++ Keywords are a list of reserved words for this programming language. Each keyword has a special meaning and it can’t be changed by the programmer. The list of the C++ keywords are:

 asm  else  new  this
 auto  enum  operator  throw
 bool  explicit  private  true
 break  export  protected  try
 case  extern  public  typedef
 catch  false  register  typeid
 char  float  reinterpret_cast  typename
 class  for  return  union
 const  friend  short  unsigned
 const_cast  goto  signed  using
 continue  if  sizeof  virtual
 default  inline  static  void
 delete  int  static_cast  volatile
 do  long  struct  wchar_t
 double  mutable  switch  while
 dynamic_cast  namespace  template

 

Now the above list of keywords doesn’t tell you anything. Do not worry about it. It’s absolutely normal because we will learn the meaning and usage of the most important part of C++ keywords and C++ Basic Syntax in this tutorial. You just have to remember that we can’t use these keywords to name constants, variables, or any other identifier’s name.

Now, let’s take a look at the program written in the previous article:

Try It

//include a header file from Standard Library
#include <iostream>
using namespace std;
//the work of the program starts from function called 'main'
int main()
{
	//use standard(console) to output  message "Hello World"
	cout  << "Hello  world" << endl;
	//wait for user to press a key
	cin.ignore();
	//return a value to the system  when program finishes its execution successfully
	return 0;
}

This is a basic program and now we will explain the basic syntax used in this program.


What is #include ?

The program always starts with some lines containing include keywords. The include keyword is preceded by the “#” symbol. The symbol “#” means that it is a preprocessor directive. This will be discussed later in the “C++ Advanced” section of this tutorial. It is important to know that if you want to use any file from Standard Library, your include line should look like this:

#include

 

What is ‘using namespace std’?

By writing “using namespace <some_namespace>” we can use that namespace in our program. For example, if we want to use “std” namespace then we have to write below line

usingnamespace std;

If you want to use any symbol from std namespace by writing the below the line, all symbols in that namespace will become visible without adding the namespace prefix. A symbol may be for instance a function, class, or variable. So after writing “using namespace std” the symbol “cout” can be used directly as shown below:

cout<<"Welcome to TutorialCup";

But if you are not writing “using namespace std” then to get above output, you need to write code as below

std::cout<<"Welcome to TutorialCup";

 

What is a main() function in C++ ?

Any function in C++ is a block of the instructions to the compiler. The block of instructions is “highlighted” within “{ }” characters. For example:

#include <iostream>
int main()
{//start of block

	//block of instructions for main  functions

}//end of block

The main function is the starting function of any C++ program. The compiler first find and execute the main function in the program.

We will discuss the C++ Basic Syntax step by step in the next articles. Now it’s just a basic explanation of the HelloWorld program and details about C++ Keywords.

Reference

Translate »