C - CONSTANTS:
Constant in ‘C’ refer to fixed value that do not change during the
execution of a program. C supports several types of constants as:
1. Numeric constant
i.
Integer constant
ii.
Decimal integer constant
iii.
Octal integer constant
iv.
Hexadecimal integer constant
v.
Real constant
1. Character constant
i.
Single character constant
ii.
String constant
iii.
Backslash character constant
|
1. Numeric Constant - Who consist of digits and some significant character e.g. E, e,
X, x, A, a, B, b, C, c, D, d, F, f, U, UN are the numeric constants.
These are of two types
(i) Integer Constant – An integer constant
refers to a sequence of digits without fractional part. It must have at least
one digit and may contain either + or – sign. A number with no sign is assumed
to be positive. There are three types of integer constant namely:
(a) Decimal
(b) Octal
(c) Hexadecimal
(a) Decimal integer constant – it consist of a set of digits, o to 9 preceded by an
optional - or + sign.
Ex. 123, 0,
+78, -321, 54543, -07.
Qualifiers (U, L, UL) – The largest integer value that can be stored is
machine-dependent. It is 23767 on 16-bit m/c and 2,147,483,647 on 32-bit m/c.
It is also possible to store large integer constants on these machines by
appending qualifiers such as U or u, L
or l and UL or ul to the
constants e.g.
For 16-Bit machine
these are valid –
56789U or 56789u (unsigned
integer)
98761234UL or 98761234ul (unsigned
long integer)
9876543L or 9876545l (long
integer)
(b) Octal integer
constant – an octal integer constant consist of any combination of digits from the set
0 to 7 with a leading zero.
Ex. 037, 0342. 03423, 07
etc.
Note - 049 is not
valid octal integer constant because 9 does not comes into octal number system.
(c) Hexadecimal inter constant – a sequence of digits preceded by zero and x (0x
or 0X) is considered as hexadecimal integer constant. They may also include
alphabets A to F or a to f where (A=10, B=11, C=12, D=13, E=14, F=15).
Ex. 0x2 or 0X2, 0x9f or 0X9F.
(ii) Real constant – These quantities are
represented by numbers containing fractional parts like 17.231. Such number are
called real constant or floating point constants.
Ex.
0.00083, -0.75,
243.36, +246.0 etc.
215.0 = 215,
0.95 = .95, -0.75 = – .75, + 0.5
= + .5 - all of these are valid.
Representing
a real constant in exponent form:
A real number may also be expressed in exponential notation (scientific
notation).
The General form
mantissa e
exponent
The mantissa is either a real number expressed in decimal notation or an
integer. The exponent is an integer number with an optional + or – sign. The
letter e or E separating the mantissa.
Ex. 213.65 = 2.1365 e2 or
2.1365E2 (e2
means multiply by 102)
0.00032351 = 3.2351 e-4 or
3.2351E-4 (e4 means multiply by 104)
2. Character Constant – A character
constant consist of either single alphabet, a single digit, or a single special
symbol enclosed with single inverted
commas (‘ ‘) or combination of these characters enclosed with double quotation mark ( “ “ ) are the
character constants.
These are three types:
(a) Single character constant – a single character constant (or simply character
constant) contains a single character enclosed within a pair or single quote
marks.
E.g. ‘5’ , ‘A’ ,
‘.’ ‘ ‘ etc.
Note: the
character constant ‘5’ is not the same as number 5. it has integer value known
as ASCII value.
E.g.:
printf(“%d %d %d “,
‘a’, ‘5’, ‘A’); will give the output
: 97, 53 and 65 as well as
printf(“%c
%c %c ”, 97, 53, 65); will give the output : a ,
5 and A.
(b) String constant – A string constant is a sequence of characters enclosed in double quotes.
The character may be letters, numbers, special characters and blank space. E.g.
“Hello” , “1943”
, “?...” , “5+4”
, “A” etc.
Note – ‘A’ is the single character constant and have an integer value 65.
“A” is the string constant and does not have any integer value.
Every sting constant is automatically
terminated with a special character ‘\0’ (back slash zero) called the null
character which represents the end of the string. this is not a separate escape
sequence, but an octal escape sequence with a single octal digit of 0; as a
consequence, \0 must not be followed by any of the digits 0 through 7;
otherwise it is interpreted as the start of a longer octal escape sequence. A
null character is used to mark the end of a character string.
For example, “hello” will represent “hello\0 ” in the memory.
Thus, the size of the string is the total number of characters plus one for
the null character.
In C, a string of
characters is stored in successive elements of a character array and terminated
by the NULL character. For example, the string "Hello" is stored in a character array, msg[], as
follows:
Declaration:
char msg[6]=”Hellw”;
Representation:
msg[0] = 'H';
msg[1] = 'e';
msg[2] = 'l';
msg[3] = 'l';
msg[4] = 'o';
msg[5] = '\0';
(c) Backslash character constant (Escape Characters/ Escape
Sequences) – C supports some special backslash character constants that are used in
output functions. For example the symbol ‘\n’ stands for new line character. A
list of thee character constant is given below:
SN
|
Const.
|
Meaning
|
1.
|
‘\a’
|
Audiable alert (bell)
|
2.
|
‘\b’
|
Back space
|
3.
|
‘\f’
|
Form feed
|
4.
|
‘\n’
|
New line
|
5.
|
‘\r’
|
Carriage return
|
6.
|
‘\t’
|
Horizontal tab
|
7.
|
‘\v’
|
Vertical tab
|
8.
|
‘ \’ ‘
|
Single quote
|
9.
|
‘ \” ‘
|
Double quote
|
10.
|
‘\?’
|
Question mark
|
11.
|
‘\\’
|
Back slash
|
12.
|
‘\0’
|
Null
|
Ex. Suppose we want to the output as:
Hello “C Program”
For this – printf(“Hello \”C Program \””);
NOTE: An escape sequence consumes only one
byte of space as it represents a single character.
No comments:
Post a Comment