C++ basics

The following characters are used in C++ for writing the program code :

Digits : 0-9

Letters: A-Z , a-z

Unclassified symbols : + , - , / , * , ( , ) , { , ,} , [ , ] , = ,  etc

                                          Blank space , tabs , newline etc

                                          The 256 ASCII characters

Token or lexical unit

In any language say English the smallest unit or entity is the letters which we use in te same way .token can be defined as the smallest individual unit of a program which can exist independently.following are the tokens used in C++  and for explaining these tokens I will make use of the following program.

#include<iostream.h>

void main()

{

const int d=4;

int a,b,c;

cout<<"enter the number A:";

cin>>a;

cout<<"enter the number B:";

cin>>b;

c=a+b;

cout<<"sum="<<c;

}

This is a program that prints the sum of the numbers entered on the screen

I.                   Tokens

A) Keywords

These are reserved words in C++.That means these words convey a special meaning to the compiler and can not be used as memory location (identifiers) for storing data .in the above program the word include , int etc are keywords as they convey special meaning.

For example: int , float , include , enum , while , for , struct , class , pointer , etc.

Names containing double underscore (__) are also reserved for its implementation and standard libraries.

 

B) Identifier

These are the building block of any C++ program. These are generally used for naming the various variables , functions , classes , pointers , etc.These can have any name as discussed in valid names in C++ in next chapter. In the above program the words a , b , c are the identifier as they are used to name the numbers entered by the user.

For example: sam ,  sam456 , main (main is an identifier) , fgfggggfgf5675877sdgsdg etc.

Please read the valid names in C++ in next chapter to understand the valid names which a programmer can use.

 

C)Literals

These are the objects or the variables that do not change their value during the program’s runtime.in the above program d is a literal i.e. of the decimal integer constant. Their are the following types of literals:

 

1)  Integer constant

These are those numbers which do not have a fractional part and that do not change their value during runtime.their are mainly three types of integer constant.

a)              Decimal integer constant

These are the integer having the sequence digits consisting of numbers from 0 to 9.

For example: 123 , 45 , -78 , +5 etc.

b)              Octal integer constants

 These are the integer having the sequence digits consisting of numbers from 0 to 7.

For example: 014 , 074 , 456 etc

c)               Hexadecimal integer constants

These are the integer having the sequence digits consisting of numbers from 0 to 9 and A to G.

For example : 12A , ABC , 2345 etc

1)  Character constants

These are the characters enclosed in single quotes .these should contain only one character and in single quotes.

For example : ‘a’ , ‘b’ etc.

2)  Floating constants

These are the numbers which have a fractional part and do not change their value during the entire runtime.

For example : 12.45 , 0.45 , -56.3 etc.

3)  String constants

These are set of multiple characters enclosed in double quotes.

For example : “abx” , “a” etc.

 

A) Punctuators

These are the symbols which make the program more collected and ordered.these are also known as separators. In the above program the punctuators used are { , } , , , ; , ( , ) etc

For example : [ , ] , ( , ) , ; , = etc

 

B) Operators

These are the symbols which perform mathematical or manipulative or computation on the variables or operands on which they are appied.in the above program + , = etc are the operator used. Operators are of following types:

1)  Arithmetic operators

Which perform mathematic operations on operands.

For example :  + , - , * , / etc.

2)  Shift operators

For example : >> , << etc.

3)  Bitwise operators

For example : &(bitwise AND) , ^(bitwise XOR) , |(bitwise OR) etc.

4)  Logical operators

These perform logical operations on two different operants .these are generally used to combine two different conditional statements.

For example : && (logical AND) , || (logical OR) etc

5)  Assignment operators

These are used to assign or give the value to a variable.

For example : + , - , * , / , += , -= , *= , etc

6)  Relational operators

These are used to check relation or perform computation between teo operands.these are generally used for writing condition statements.

For example : < , > , <= , >= , == , !=  etc

7)  Component selection operators

For example :  . (direct component selection) , -> (indirect component selection)

8)  Class member operators

For example :  :: (scope access/resolution),  .* (deference pointer to class member) ,      ->* (deference pointer to class member)

9)  Conditional operator

For example :  ? , : etc.

 

 

 

 

Make a Free Website with Yola.