Vous êtes sur la page 1sur 7

Tokens - C++ Declarations - Pearson - Programming in C++

1 of 7

http://gradestack.com/Programming-in-C-/C-Declarations/Tokens/2119...
Explore (/mainsite/explorecourse)

(http://gradestack.com/)

Login (/web/login)

Tokens
The smallest individual unit in a program is known as token.
C++ programs contain various components. The compiler
identifies them as tokens. Tokens are classified into the
following types and are also indicated in Figure.
1. Keywords Reserved by the compiler.
2. Identifiers Names of variables, arrays, classes,
functions, etc. are identifiers.
3. Constants Constants are fixed values.
4. Operators Operators are different types and are used
in expressions.
5. String Sequence of characters.
The keywords are reserved set of words with fixed meanings.
The variables are used to hold data temporarily. The
operators are used to perform different operations such as
arithmetic and logical. Values such as 1, 5, 2.5, etc. are
known as constants. The operators such as #, ?, and ~ are
known as special characters. The # is used for preprocessor
directive; ? is a conditional operator; and ~ is used for bitwise
operation. The detailed descriptions of these tokens are
described in following sections.

Types of tokens
Comment

Like 0

Share

Keywords
The C++ keywords are reserved words by the compiler and
are assigned fixed meanings. All C keywords are valid in
C++. The programmer may not apply them in the programs
for defining variable names; however, few C++ compilers
permit to declare variable names that exactly match with the
keywords.
The common keywords between C and C++ are listed in Table
4.1 and the additional keywords of C++ are listed in Table 4.2.
These keywords are used with classes, templates, and
exception handling, etc. Table 4.3 contains keywords added
by ANSI committee. Table 4.4 contains additional keywords
provided by Turbo C++ compiler.

Table 4.1 C and C++ Common Keywords


auto

double

int

struct

break

else

long

switch

case

enum

register

typedef

char

extern

return

union

const

float

short

unsigned

continue

for

signed

void

default

goto

sizeof

volatile

do

if

static

while

Table 4.2 Additional C++ Keywords


asm

private

catch

protected

class

public

delete

template

friend

this

inline

throw

new

try

operator

virtual

Table 4.3 Keywords Added by ANSI Committee


Keywords Added by Ansi Committee
bool

const_cast

mutable

namespace reinterpret_cast static_cast false

typename using

dynamic_cast
wchar_t

explicit
export

true
typeid

03-Jul-16 11:21 AM

Tokens - C++ Declarations - Pearson - Programming in C++


Tokens

2 of 7

http://gradestack.com/Programming-in-C-/C-Declarations/Tokens/2119...

Table 4.4 Additional Keywords in Turbo C++


Additional keywords in Turbo C++
cdecl

_cs

_ds

_es

_fastcall

huge

interrupt

_loads

pascal

_saveregs

_seg

_ss

Comment

far
near

Like 0

Share

Identifiers
Each variable is represented by a symbol using identifiers.
Identifiers are names of variables, functions, arrays, etc. In
other words, identifiers refer to variety of entities such as
structures, unions, enumeration, constants, typedef names,
functions, and objects (refer Figure).

Types of tokens

C++ identifier always starts with an alphabet and it is a plain


sequence of alphabets and/or digits. C identifier does not
allow blank spaces, punctuation, signs, etc.
Identifiers are user-defined names and are generally defined
in lower case letters. However, the upper case letters are also
permitted. The underscore (_) symbol can be used as an
identifier. In general, underscore is used to link two words for
the long identifiers. Valid identifiers are as follows:

length, area, volume, sUM, Average, etc.


Invalid identifiers are as follows:
Length of line, S+um, years, etc.

Rules for defining identifiers are as follows:


1. The identifier name must begin with a character or
underscore and should not start with digit. No spaces
are allowed between the characters in the variable but
underscore is allowed.
2. The identifier name should not be a C++ keyword.
3. The identifier name can be a combination of upper
and lower characters. For example, the variables suM,
sum, and SuM are not same.

Comment

Like 0

Share

Constants
The constants in C++ are applicable to the values that do
not change during execution of a program. There are several
types of constants in C++. They are classified into the
following groups as shown in Figure 4.3 and Table 4.5.

C++ constants

Table 4.5 Constant Types


Example

Constant Type

542

Integer constant

35.254

Floating point constant

0x54

Hexadecimal integer constant

0171

Octal integer constant

Character constant

cpp

String constant

Numerical Constants

Integer Constants:
Integer constants are represented with whole numbers. It
requires minimum two bytes and maximum four bytes of
memory.

The following concepts are essential for the numerical


constants:
1. The numerical constants are represented with
numerical numbers. At least one digit is needed for
representing the numerical number.
2. The decimal point or fractional part or any other
symbols are not permitted. Neither even blank spaces
nor commas are permitted.

03-Jul-16 11:21 AM

Tokens - C++ Declarations - Pearson - Programming in C++


Tokens

3 of 7

http://gradestack.com/Programming-in-C-/C-Declarations/Tokens/2119...

3. Integer constant could be either positive or negative or


may be zero.
4. The number without a sign is assumed to be positive.

Valid Examples: 10, 20, +30, 15, etc.

Invalid integer constants: 2.3, .235, $76, 3*^6, etc.


Integers can be represented in octal or hexadecimal based on
the requirement, besides decimals.
Octal has base 8 and hexadecimal 16. The octal numbers are
0, 1, 2, 3, 4, 5, 6, 7, and hexadecimal numbers are 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, A, B, C, D, E, F.
The representation of octal numbers in C++ would be done
with leading digit 0 and for hexadecimal with leading 0X.
Following are few examples of octal and hexadecimal
numbers:
027, 037, 072 octal numbers
0X9, 0Xab, 0X4 hexadecimal
Various constants are given in Table 4.5.
Real Constants:

Real constants are often known as floating point constants


and can be represented in exponential or fractional form.
Integer constants are unfit to represent many quantities.
Many parameters or quantities are defined not only in
integers but also in real numbers. For example, length, height,
prize, distance are also measured in real numbers.

The following concepts are essential for real numbers.


1. Decimal point is permitted.
2. Neither blank spaces nor commas are permitted.
3. Real numbers could be either positive or negative.
4. Number without a sign is assumed to be positive.
Examples of real numbers are as follows:
2.5, 5.521, 3.14.
The real constants can be written in exponential notation,
which contains fractional and exponential parts. For
example, the value 2456.123 can be written as 2.4561 X e+3.
The part that precedes e is called as mantissa and following
it is an exponent.
In this example 2.4561 is mantissa and +3 is exponent.
Following points must be noted while constructing a real
number in exponential form:
1. The real number contains mantissa and exponent.
2. The letter e separates the mantissa and exponent
and it can be written either in upper or in lower case.
3. The mantissa is either a real number represented
either as decimal or as an integer.
4. The mantissa may be either positive or negative.
5. The exponent is an integer which may be either
positive or negative.
Valid examples are 5.2e2, -2, 5.0e-5, 0.5e-3, etc.
Also in double type, the real numbers can be expressed with
mantissa and exponent parts.
Character Constants

Single-character constants:

A character constant in C is represented with a single


character and it is enclosed within single quotes. Each
character constant has a particular integer value associated
it.
Each character is represented with different ASCII value (the
values are listed in Appendix 1). A character can also be
represented with single digit or single special symbol or
white space, all enclosed within a pair of single quote marks.

Example: a, 8, , &, etc.


String constant:

String constants are sequence of characters enclosed within


double quote marks. The string may be combination of all
types of symbols.

Example: Hello, India, 444, a.

In short, the various constants are shown in Table 4.5.

A programming example for various constants is as follows:


4.1 Write a program to demonstrate various constants.

03-Jul-16 11:21 AM

Tokens - C++ Declarations - Pearson - Programming in C++

http://gradestack.com/Programming-in-C-/C-Declarations/Tokens/2119...

Tokens

4 of 7

Explanation: In this programming example, various


constants are assigned to different variables.
Symbolic Constants

A symbolic constant is defined in the same way as a


variable. However, once the constant is initialized, the
assigned value cannot be altered.

The constant can be defined in three ways: #define, const


keyword, enum keyword

#define preprocessor directive: The #define preprocessor


directive can be used for defining constant. The symbols
defined using #define are called macros. The syntax of
#define is as follows.
#define name constant_value

Explanation: #define price 152


In the above example, price symbolic constant contains 152
and data type, such as int, float, or char, is not mentioned.
Every time when the preprocessor finds the word price, it
substitutes with 152.

4.2 Write a program to find the radius and circumference of


the circle using #define directive.

Explanation: In the above program, the value of pi is replaced


by 3.14 during program execution. In the program instead of
writing the value of pi as 3.14 we define directly the value pi
as 3.14. The term pi is used for calculating the area and
circumference of the circle.
Constant using const keyword:

Example:
const int price=152;
The above declaration defines a constant of type int. Here,
data type is strictly maintained while execution and the value
of constant cannot be changed during run-time.
4.3 Program on const keyword.

03-Jul-16 11:21 AM

Tokens - C++ Declarations - Pearson - Programming in C++

http://gradestack.com/Programming-in-C-/C-Declarations/Tokens/2119...

Tokens

5 of 7

Explanation: A constant value is assigned to the variable x


and the same is displayed. If attempts are made to modify
constant value, compiler shows an error (cannot modify the
constant).
Constant Pointers
C++ allows us to create both constant pointer and pointer to
constant. Consider the following example.
1. Constant pointer: It is not possible to modify the
address of the constant pointer.

Example:

char * const str=Constant;

In the above example, it is not possible to modify the


address of the pointer str. Thus, the following
operations will generate error.

// str=san; // cannot modify a constant object

// ++str; // cannot modify a constant object


2. Pointer to constant: If pointer is declared to constant,
only the value can be changed using actual variable
which is not possible using pointer.

Example:

int const * pm=&k;

In the above example, pm is declared as pointer to


constant. The following examples are possible and
invalid operations.

// k=5; // possible

// *pm=5 // cannot modify the constant object

// pm++; // possible

// ++ *pm; // cannot modify a constant object

// *pm=5; // cannot modify a constant object


3. Pointer and variable both constants:

Example:

const char * const p=ABC;

In the above example, both the pointer and variable


are constants. Hence, it is not possible to change the
value and address of the pointer.
4.4 Write a program to define constant pointer and pointer to
constant. Perform the possible operations.

4. Constants can be defined using enum as follows:

Example:

enum {a,b,c};

where a,b, and c are declared as integer constants


with values 0, 1, and 2, respectively.

We can also assign new values to a, b, and c.

enum [a=5,b=10,c=15};

03-Jul-16 11:21 AM

Tokens - C++ Declarations - Pearson - Programming in C++


Tokens

6 of 7

http://gradestack.com/Programming-in-C-/C-Declarations/Tokens/2119...

where a, b, and c are declared as integer constants


with values 5,10,and 15, respectively.

Comment

Like 0

Share

Painting Contractors

Beautiful Homes; Trendy O ces. Free Visit. Painting Experts.


Call!

Operators
C++ supports all the operators of C. In addition, C++
introduces few more operators. The new operators are <<, >>,
::, ::*, ->*,.*, delete, new, etc.

These operators are discussed in detail at the end of this


chapter.
Comment

Like 0

Share

String Constants
String is represented by an array of characters. String
constants are enclosed in double quotes. Following are the
valid examples of string constants.

Pearson, Delhi, India, Singapore, Popular, publisher,


etc.
Comment
Previous

Like 0

Share

Next

(http://a.tribalfusion.com/h.click
/anmQwvRrZavPW 3dVG3U4b6rnWZaoYq2M4dMCPGZbZc4AJIodPOVHJhYbviYbbk1qqpRrMCUUQSWdQ0nFBpRUjpYars5T3c5qvRoEBIXFU9WHjXmPQCmV7mmW 7A3aZbg5tEq56rZbmFbZd0GnWYVF10sb
/http://www.cidi.org
/NepalRelief?utm_medium=banner&
utm_source=exponential&
utm_campaign=adcouncil&
utm_content=nepal)

(http://a.tribalfusion.com/h.click
/aamQwv1cnVXVFwmEBT3rFRWrJDVAU0REM2PcrOQHUw1tnxTmMp3sQWXbnZcU6iq4AvdP6jK3HUo1dQIpdEO5AnS4GUaVVYjUsB8S6FuW dv3UbFS2r2oUabmVqJaSTrZdSs3JQFuoPHv7W cbR4ryxmtqq0q
/http://www.cidi.org
/NepalRelief?utm_medium=banner&
utm_source=exponential&
utm_campaign=adcouncil&
utm_content=nepal)

03-Jul-16 11:21 AM

Tokens - C++ Declarations - Pearson - Programming in C++


Tokens

7 of 7

About Us (/mainsite/aboutus)
Contact Us (/mainsite/contact)
Developers (/mainsite/developer)

http://gradestack.com/Programming-in-C-/C-Declarations/Tokens/2119...
News (/mainsite/news)

Blog (/blog/)

Terms and Conditions (/mainsite/policy)


Publishers (/mainsite/publisher)
mail us at contact@gradestack.com (mailto:contact@gradestack.com)

03-Jul-16 11:21 AM

Vous aimerez peut-être aussi