SEARCH THIS BLOG

TOKENS IN C++


In this article we will learn about C++ Tokens.


The smallest individual unit in a program that has some meaning to the compiler is called a token.


C++ has the following tokens:


Keywords

Identifiers (commonly known as Variables)

Literals (commonly known as Constants)

Punctuators

Operators



Keywords:

Keywords are reserved words in C++ which have predefined special meaning to the language compiler. These keywords need to be used in correct syntactical order.


Download list of keywords in C++11. (pdf)

 


Identifiers:

 

Identifiers are commonly known as Variables. It is a named memory location that can store one value at a time and these values can be changed at the time of execution of the program.

C++ is a case sensitive language and so two similar variables in different character case are considered as two separate variables.

It is important to keep in mind the rules for naming a variable in C++ to avoid any compiler error.


Rules for naming a Variable:


  • Variable name can contain letters and digits only.


  • No special character or symbol is allowed in variable name, not even space.

 

  • A variable name cannot begin with a digit but digits can be used anywhere else in the name.

 

  • _(underscore) is the only allowed special character in Variable name and it can be used anywhere in the name (also at beginning).


Examples of some valid variables:


Myfile, DATA9_7, FILE68, Z2ToZ, _DS, _2Check

 

Following are invalid variable names:

DATA-REC     contains special character (-)

29CLCT           starts with a digit

break                reserved keyword     

My.file             contains special character(.)

 

Literals:

 

Literals (often referred to as constants) are data items that never change their value during a program run.

C++ allows the following kinds of literals:


Integer-constant:

These are integers, i.e. numbers without any fractional part. Integers are written according to the following rules:

First, It must contain at least one digit.

Second, It must not contain any decimal point

Third, It may contain either + or – sign. An integer with no signs is assumed as positive.

Fourth, Commas are not allowed in Integer constants.


There are 3 types of Integer constants:


Decimal Integer Constants : An integer constant consisting of a sequence of        
digits is taken as a decimal integer constant unless beginning with 0.

Example: 1234, -69, +97

Octal Integer Constants : A sequence of digits starting with 0 (zero) is taken to be an octal integer.

Example: 0123, 047

Hexadecimal Integer Constants : A sequence of digits starting with 0x or 0X is taken to be hexadecimal integer constant.

Example: 0xA, 0X12C

         

Read more>> Integer constants in C++  


Character Constants:


A character constant is single character enclosed in single quotes, as in ‘z’. The rule for writing a character constant is that it must contain one character enclosed in single quotation marks.


If more than one characters are included within a single quote, the compiler takes the rightmost character as the value for that constant.


Example: In character constant ‘abc’, the compiler takes the constant as ‘c’ and allocates 1 byte space.

Even non graphic characters can be represented by using escape sequences. An escape sequence is represented by a backslash(\) followed by one or more characters. The following table gives a listing of escape sequences:

 

Escape Sequence

Nongraphic Character

\a

\b

\f

\n

\r

\t

\v

\\

\’

\”

\?

\On

\xHn

\0

Audible bell (alert)

Backspace

Formfeed

Newline or linefeed

Carriage return

Horizontal tab

Vertical tab

Backslash

Single quote

Double quote

Question mark

Octal number

Hexadecimal number

Null character



Note: Escape sequences are single characters and so take only 1 byte of space in memory.


Floating Constants:


Floating constants are also called real constants. These are numbers having fractional parts. They may be written in one of the two forms, viz. fractional form and exponential form.


A real constant in fractional form consists of signed or unsigned digits including a decimal point between the digits. 

The rules for writing a real constant in fractional form are:

first, It must have at least one zero before and after the decimal point.

second, It may be positive or negative depending on sign. An unsigned real constant is assumed to be positive.

third, Commas or any other special character is not allowed.


Examples of valid real constants in fractional form are:

2.0, 17.5, -13.0, -0.00065


Examples of invalid real constants in fractional form:

 7                (No decimal point)

7.                (No digit after decimal)

.7                (No digit before decimal)

17,233.6    (Commas are not allowed)


A real constant in exponent form consists of two parts : mantissa and exponent. For instance, 5.8 can be written as 0.58 x 101 = 0.58E01 where mantissa part is 0.58 (i.e. the part before E) and exponent part is 1 (i.e. the number after E). E01 represents 101. The rules are:


first, The mantissa must be either an integer or a proper real constant.

second, The mantissa must be followed by a letter E or e and the exponent.

thirdThe exponent must be an integer.

fourth,Commas and special characters are not allowed.


Valid real constants in exponent form :

125E02, 14.2E05, 0.25E06, 0.25E+8, 2.5E-3


Invalid real constant in exponent form :

172.E5        (No digit after decimal)

1.7E            (No digit after E)

0.17E2.3    (Exponent must be integer)

17,2E02     (Comma not allowed)

 


String Literals:


Multiple Character constants enclosed within double quotes are treated as string literals. A string literal can be anything enclosed in double quotes (including digits, alphabets and special symbols).


One important thing in case of string literals is the use of NULL character (‘\0’). Each string literal is automatically added with a terminating character ‘\0’. Thus, the string “abc” is actually read by compiler as “abc\0” and so occupies 4 bytes of space and not 3.

 

Punctuators :

 

The following characters are used as punctuators (also known as separators) in C++ :

[ ]  ( )  { }  , ;    :   *     =   #

 

These are symbols that enhance a program’s readability and give proper meaning to statements, expressions etc. as per the syntax.

 

Operators :

 

Operators are tokens that trigger some computational actions when applied to variables and other objects in an expression.

 

The various operators defined in C++ are :

I/O Operators :   << and >>

Arithmetic Operators  :   + - * / %

Relational Operators   :   <  >  <=   >=   ==  !=

Logical Operators         :   &&  ||  !

Increment/Decrement Operators  :  ++   --

Bit wise Operators    :    &  ~  |   ^

Conditional Operator :  ?:


Read more>> Operators in C++ 

 

 

 

Do not stop here. Read more articles related to C++ here.

Download full pdf of this page to read at your own convenience. click here

If you have any doubts, queries or suggestions for the website, please comment on this post.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

 

Read More:

Operators inC++

Data types in C++

Variables inC++


Post a Comment