C++ Programming Basic Syntax


C++ Programming Basic Syntax

The C++ is a superset of C and therefore most of the constructs of C are legal in C++ with some exceptions and additions. The Tokens are the smallest individual element of C++ programming and it constructs the basic syntax of C++. The token may be either an identifier, any special character, keyword or blocks etc. So, the basic syntax of C++ includes-

Semicolons and Block in C++ Programming

Like C, in C++ also, each statement must be ended with a semicolon (;). Hence, the semicolon is the terminator of each statement in C++ programming. For example:

int a;
cout<<“Introduction to C++”<<endl;

 

The Block in C++ refers to a single/set of statement within the opening braces ( { ) and closing braces ( } ). For example:

{
cout<<“Introduction to C++”<<endl;
return 0;
}

 

Identifiers in C ++ Programming

The identifier used in C++ is similar to the C which refers to the name of the variables, functions, array, structure, class etc. created by the programmer. It has certain rules which are common to both C and C++. These are-

  • The identifiers can be written by using alphabets, numeric character and underscore only. Other symbols are not permitted.
  • An inbuilt keyword cannot be used as identifier. For example: class, int etc.
  • The identifier cannot start with a numeric character. For example: int 1num; is not acceptable but int num1; is acceptable.
  • The uppercase and lowercase letters have different meaning. For example: int num; and int NUM; is different i.e. num and NUM are 2 different identifiers.

Keywords in C++ Programming

Keywords are the inbuilt reserved identifier and can not used as a name of variable or constant by the programmer. As in C, C++ also has a list of keywords as follows-

KEYWORDS IN C++
auto double new switch
break else operator templates
bool enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while

 

KEYWORD ADDED BY ANSI C++
const_cast dynamic_cast explicit
export false mutable
namespace static_cast true
typename using wchar_t

 

Whitespace in C++ Programming

The whitespace is used in C++ to refer to the blank space, tabs, newline character or comments. It separates the elements from one another in a statement and enables the compiler to identify where an element started, end or next element begins. The line containing only whitespace, possibly along with the comment is known as Blank Line. There must be at least one white space character between every element of the program so that the compiler being able to distinguish each element. For example:

int a;

 

But if there is an operator and operand or any other symbol, than there is no need of single space also. For example:

Num=num1+num2;

 

Comments in C++ Programming

Comments are one of the basic syntax used in C++ programming. The comments are not compiled by the compiler. Comments are used to describe the program for future use or better understanding which improves the readability and understanding of the program code. The comments may contain a single line or a set of lines.

  • For single line comment, ” \\ ” is used before writing the comment.
  • For multiple line comment, the comment is written between the /* and */.

For example:

//This is single line comment

/*This is 1st line of multiple line comment
This is 2nd line of multiple line comment
…………………………….
This is last line of multiple line comment*/

 

Special Character in C++

some of the special characters are used in C++ to describe different elements of the program. These are-

  • Pound sign (#): It marks the beginning of a preprocessor directive.
  • Opening and closing braces ( <> ) : It is used to enclose the filename in the #include header file.
  • Opening and closing quotation marks ( ” ” ) : It is used to encloses a string of characters, such as a message that is to be printed on the screen.
Close Menu