Literals (often referred to as constants) are data items that
never change their value during a program run.
C++ allows several kinds of literals:
Integer –
constant
|
Character-
constant
|
Floating-
constant
|
String-
literal
|
INTEGER – CONSTANT
Integer constants are whole number without any fraction part.
The method of writing integer constants has been specified in the following
C++ allows THREE types of integer constants:
- · DECIMAL ( base 8 )
- · OCTAL ( base 8 )
- · HEXADECIMAL ( base 16 )
DECIMAL INTEGER CONSTNATS
An integer constant consisting of a sequence of digits is
taken to be decimal integer constant unless it begins with 0. For instance
1234, 41 +97, -17 are decimal integer constants
OCTAL INTEGER CONSTANTS
A sequence f digits starting with 0 is taken to be an octal
integer. Ex:- decimal integer 8 will be written as 010 as octal integer. And decimal
integer 12 will be written as 014. When you start a number with 0 make sure
that digit 8 or 9 is not included octal numbers recognize digits 0-7 only.
Numbers like 091 or 068 or 023 etc. are illegal numbers and program may behave
erratically upon encounter such numbers.
HEXADECIMAL INTEGER CONSTANTS
A sequence of digits preceded by 0x or 0X is take to be an
hexadecimal integer. Decimal 12 will be written as 0XC as hexadecimal integer.
Thus number 12 can be written either as 12(as decimal) or 014
(octal) or 0XC (hexadecimal).
CHARACTER-CONSTANTS
A Character constant is single character enclosed in single
quotes as in ‘z’.
Even non-graphic character can be represented by using escape
sequence. An escape sequence is represented by a backslash (\) followed
by one or more characters.
FLOATING –CONSTANTS
Floating constants are also called real constants. Real
constants are number having fraction parts. These may be written in one of the
two forms called fraction form or the exponent form.
A real constants in fraction form consist of signed or
unsigned digits including a decimal pints between digits.
The following are valid real constants in fraction form
2.0 , 17.5 , -130 , -0.00625.
The following are the valid constants in exponent form
152E5 , 1.5E07 , 0.172E-3 , 152E+8.
STRING-LITERALS
Multiple character constants can be dealt with in two ways in
C++. If enclosed in single quotes, these are treated as character constants ad
if enclosed In double quotes these are treated as string- literals.
Each string literal is automatically added with a terminating
character ‘/0’. This, the string “abc” will actually be represented as “abc/0”
in the memory and its size is not 3 but 4 characters.
0 comments:
Post a Comment