CONSTANT
There
are three type of constants: string constants, numeric constants and character
constants.
Constants -
string constants
- Numeric constants
- Character constants
String
Constants
A
String constants is a sequence of alphanumeric characters enclosed in double
quotation marks whose maximum length is 255 characters. Following are the
examples of valid string constants.
(1)
″ the
result = ״
(2)
″ Rs 2000.00″
(3)
″ This is test program by Ravic″
Following
are some examples for invalid string constants.
Ravichandran - Characters are not enclosed
in double quotation marks.
“ My name is %$% - Closing double quotation mark is missing.
‘this is test’ - Characters are not enclosed
in double quotation marks.
Numeric Constants
Numeric constants are positive or negative numbers. There
are four types of numeric constants: integer constants, floating point
constant, hex constant and octal constants. An stant may either be of single
precision or double precision.
- Integer
Numeric
constants Integer - Short
integer integer (short)
- Long integer (long)
Float
- Single precision (float)
- Double precision (double)
- Long double
- Unsigned char
- Unsigned integer
Unsigned - Unsigned short integer
- Unsigned long integer
Hex - Short hexadecimal l
- Long hexadecimal
Octal - Short octal
- Long octal
(a)
Integer
constants Integer constants do not
contain decimal points. Variables can be declared as integers in the following
ways.
int x,y;
short int x , y ;
long int x , y ;
In the first statement, the
variables x
and y
take integer
values. In the second statement x, y are of the short
integer type.
The third statement declares x, y as long integer type variables.
(i
) Integer data : The keyword ‘int’ stands for the integer data
type in C++ and its size is either 16 or 32 bits. A 16 bit integer may fall in
the range of -215 to 215
-1, while 32 bits integers may fall in the range of -231 to 231
-1. Integer data type can again be classified into three groups as
normal integer , short integer, and long integer types. The normal integer may
be either 16 bit or 32 bit long depending upon
the index used by the programmer.
(ii) Short integer data type : Normally, the ‘short
int’ is
use to declare the short integer data type in C++ whose maximum size is 16 bits
long. It may fall in the range between -32,768 to 32,767 or -215 to
215 -1.
(iii) Long integer data type : Usually, the ‘long
int’ stands
for the long integer data type used in C++ and its size is 32 bits. It may fall
in the range of -2,147,483,648 to 2,147,483,647 or -231 to 231
-1.
The long integer data type declared for
reading or writing of the data is identified by an appended “L” or “I”.
(iv) Unsigned data type : The unsigned
numbers are
whole numbers and always hold positive values and the sign bit also belong to
the value. The unsigned numbers may be classified into four types: unsigned
char, unsigned integer, unsigned short integer and
unsigned long integer. The maximum size of the unsigned integer is 16 or 32 bits.
The unsigned short integer can be denoted as ‘unsigned short int’
in C++ and its
size is 16 bits or its range is from 0 to 65635. The using long integer can be
declared in C++ as ‘unsigned long int’ whose maximum size is 32 bits or its range is from 0 to
4,294,976,295.
(b) Floating point constants positive or negative numbers are represented
in exponential form (similar to scientific notation). The floating point
constant consists of an optionally signed integer or fixed point
number (the mantissa) followed by the letter E and e an optionally
signed integer
(the exponent).
Examples
9010E10
7810.11E-11
-10.990e8
-1.001e-1
(i) Floating data type: The number which are stored
in the from of floating point represented with binary mantissa and exponent are
known as floating point numbers. They can be declared as ‘float’ in C++ whose maximum size is
a rational number approximately between -0.29e-38 in C++.
(ii) Double data type : ‘double’ is
a keyword to represent double precision floating point numbers in C++. The size
of ‘double’ is a rational number in the
same range as float and is stored in the form of floating point representation
with binary mantissa and exponent.
The typical floating point
types and its size in C++ are given in the following table:
Data Type
|
Size in
Bytes
|
float
double
long double
|
4
8
12 or 16
|
(c) Hex
constants Hexadecimal numbers are
numbers are integer number of base 16 and their digits are 0 to 9 and A to f ( a to
f
).
Any hexadecimal number is characterized in the
is Hex Constants group. In C++ , it is normally represented using
character x.
Here are some
examples
0x0
0x3
22x3
(d) Octal
constants Octal numbers are integer
numbers of base 8 and their digits are 0 to 7. Any octal number is
characterized in the octal constants group.
The typical
integer data types and its sizes in C++ are given in the following table:
Data Types
|
Size in Bytes
|
char
short
int
long
|
1
2
2 or 4
4 or 8
|
Character
Constants
A
character represented within single quotes denotes a character constant.
Here are some examples,
‘A’
‘a’
‘:’
‘?’
Character data type Any character belonging
to the ASCII
character set is considered as a character data type whose maximum size is 8
bits long. The char
is keyword to represent the character
data type in C++. Character constants are always represented within single
quotes. Plain char, signed char and unsigned char are three distinct types. A
char, signed char and unsigned char are three distinct types. A char, a
signed char and an unsigned char
occupy the same
amount of memory space. The classification of the char
data type is
dependent on the version of the C++ compiler.
Declaration of the character
constant
char x;
char x, y, z;
The
char stands for
the character data for declaring the character constants. The declaration
format of character data type is same as that of the declaration of integer,
floating or string constants.
The
backslash (\) is use to denote non-graphic characters and other special
characters for a specific operation. The flowing characters are used as
non-graphic characters:
‘\a’ alert bell
character
‘\n’ newline (line feed)
‘\t’ horizontal
tab
‘\b’ back space
‘\r’ carriage
return
‘\f’ form feed
‘\v’ vertical tab
‘\\’ back slash
‘\’’ single quote
‘\0’ mull
character
‘\?’ question mark
‘\000’ octal value such as \o33
‘\xhhh' hexadecimal value
such as \x1b]
Note
that the null character ‘\0’ is to be distinguished from 0 (zero, not the
alphabet ‘o’)
The following table
summarize the data types in C++
Name
|
Meaning
|
char
int
long int
short int
unsigned char
unsigned int
unsigned short int
unsigned long int
float
double
|
character
integer
long integer
(more digits)
short
integer (fewer digits)
unsigned
character
unsigned
integer
unsigned
short integer
unsigned
long integer
floating
point number
double
precision floating point number
|
c programming example codes for beginners
ReplyDeleteexample c code - Print character history