Loading....
Coupon Accepted Successfully!

 

Tokens

  • The smallest individual units in a program are known as tokens.
  • C++ has the following tokens.
  • They are
    • Keywords
    • Identifiers
    • Constants
    • Separators
    • Operators
  • Let us discuss them in detail

Keywords

  • Keywords are words which belong to C++ language.

  • They have standard predefined meaning.

  • These words should be used only for their intended purpose.

  • The users have no right to change its meaning.

  • The following table shows the various keywords

asm

Auto

break

case

catch

Char

Class

const

continue

default

Delete

do

double

else

enum

Extern

Float

for

friend

goto

if

Inline

int

long

new

Operator

Private

protected

public

register

return

Short

signed

sizeof

static

Struct

Switch

template

this

throw

Try

Typedef

union

unsigned

virtual

Void

volatile

while

   
  • Example
    • ​int a;
    • float b;​
Keywords should be written in lower cases.

Identifiers

  • Identifiers are the names given to variable, functions, array and other user defined objects.
  • They have their own rules to be followed.
  • The basic rules are
    • Identifiers are formed with alphabet, digits and a special character underscore.
    • The first character must be an alphabet.
    • No special characters are allowed other than underscore.
    • They are case sensitive. ie., AA is different from aa.
  • The following are the valid identifiers.
    • A0
    • B_1
    • Cat_2
    • Basicpay
    • BASICPAY
  • The following are the invalid identifiers.
    • AB. (period operator should not be used)
    • 98A (it should start with an alphabet)
    • auto (it is a reserved word)
No space should be left between two words in the identifier. For example "Basic pay". This is an invalid identifier

Constants

  • Constants are data items that do not change their value during a program are running.
  • There are various types of constants available.
  • They are
    • Integral constant: It contains whole number without any fractional part.
    • Floating constant: they are constants which have the floating part. They form two forms namely, fractional and exponent forms.
    • Character constant: These are constant which accepts only one character and enclosed in a single quotation. Example 'a'.
    • String literal: These are constants which accept a sequence of characters enclosed in double quotation. Example "apple".

Escape Sequences

  • These are non executable statement of the compiler.
  • It performs action directly from the keyboard.
  • It is represented by using a back slash '\'.
  • The following table gives a listing of escape sequences.

Escape sequence

Non graphic character

Escape sequence

Non graphic character

\a

Audible bell

\b

Back space

\f

Form feed

\n

New line

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

\\

Back slash

\'

Single quotation

\"

Double quotation

\?

Question mark

\On

Octal number

\xHn

Hexa decimal number

\0

null

Separators

  • These identifiers are used to perform special operations.

  • They are as follows

[]

Brackets

Opening and closing brackets indicate single and multidimensional array subscripts

()

Parentheses

It indicates function calls and function parameters

{}

Braces

Indicates the start and end of a compound statement

,

Comma

it is used as a separator in a function argument list

;

Semi colon

It is a statement terminator

:

Colon

Indicates labeled statement

*

Asterisk

Pointer declaration

(...)

Ellipsis

Used fro formal argument lists

=

Equal to sign

It is used for variable initialization and as an assignment operator in expression

#

Pound sign

Used for preprocessor directive

 

Separator is otherwise called as Punctuation.

Operators

  • Operators are used to perform various computations in the computer.
  • They are used create an expression.
  • There are two categories of operators available in C++.
  • They are
    • ​Unary operator: these are operators which require only one operator to operate upon. They are

&

Address Operator

+

Unary addition

-

Unary minus

~

Bitwise complement

++

Increment operator

--

Decrement operator

!

Logical negation

  • Binary operator: these operators that require two operands to operate upon. They are
  • Arithmetic operator: It performs all types of arithmetic calculations. They are listed below

Operator

Definition

example

+

Addition

C= a + b

-

Subtraction

C=a - b

*

Multiplication

C= a * b

/

Division

C= a/b

%

modulus

C=a % b

  • Shift operator: It performs moving the characters from one location to another. They are listed below

Operator

Definition

example

<<

Shift left

A<<3

>>

Shift right

a>>3

  • Bitwise operator: It performs operation using bit manipulation. They are listed below

Operator

Definition

example

&

Bitwise AND

a & b

|

Bitwise OR

a | b

^

Bitwise XOR

a ^ b

  • Logical operator: It is used to perform operations on expression. They are listed below

Operator

Definition

example

&&

Logical AND

a &&b

||

Logical OR

a || b

!

Logical NOT

a! b

  • Assignment operator: It is used for assigning values for a variable. They are listed below

Operator

Definition

example

=

Assignment

A = B

*=

Assigning product

A *=B

+=

Assigning sum

A+=B

-=

Assigning subtraction

A-=B

/=

Assigning division

A/=B

  • Relational operator: It performs comparison between two data. They are listed below

Operator

Definition

example

<

Less than

A < B

>

Greater than

A >B

<=

Less than equal to

A<=B

>=

Greater than equal to

A>=B

==

Equal to

A==B

!=

Not equal to

A!=B

  • Conditional operator: It performs operations using two operators, which perform simple expressions. They are listed below

Operator

Definition

?

Question mark

:

Colon

 
Note: conditional operators are used when a simple if statement to be performed. The two symbols are used together. For example if(a>b)? a:b
The example tells when a is greater than b then the result is a else the result is b.
  • Increment and decrement operator: These operators are used to increment a value or decrement a value by 1. they are listed below

Operator

Definition

example

++

Increment

A++

--

Decrement

A--

Operator Precedence

#

  • Operator precedence is nothing but the order the in which the operators are used for manipulation. i.e. for execution. They are given below.

Operator

Explanation

() []

Brackets

/

Division

*

Multiplication

+

Addition

-

subtraction

  • The above order to be followed for execution.
  • For example, the following equation is solved using the operator precedence.

= (4 +4) / 2 + 5 * 3

Step 1: perform the operation inside the bracket

= (8) /2 + 5 * 3

Step 2: perform division

= 4+ 5 * 3

Step 3: perform multiplication

= 4 + 15

Step 4: perform addition

= 19.





Test Your Skills Now!
Take a Quiz now
Reviewer Name