Explain various types of storage classes in C with example for each. 8m Jun2007
To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of data, which may be represented differently within the computer memory. The data type specifies two things:
1. Permissible range of values that it can store.
2. Memory requirement to store a data type.
C Language provides four basic data types viz. int, char, float and double. Using these, we can store data in simple ways as single elements or we can group them together and use different ways (to be discussed later) to store them as per requirement. The four basic data types are described in the following table 3.1:
Table : Basic Data Types
Memory requirements or size of data associated with a data type indicates the range of numbers that can be stored in the data item of that type.
Integer Constants
Further, these constant can be classified according to the base of the numbers as:
1. Decimal integer constants
These consist of digits 0 through 9 and first digit should not be 0.
For example,
1 443 32767
are valid decimal integer constants.
2. Invalid Decimal integer Constants
12 ,45 , not allowed
36.0 Illegal char.
1 010 Blankspace not allowed
10 – 10 Illegal char –
0900 The first digit should not be a zero
3. Octal integer constants
These consist of digits 0 through 7. The first digit must be zero in order to
identify the constant as an octal number.
Valid Octal INTEGER constants are:
0 01 0743 0777
Invalid Octal integer constants are:
743 does not begin with 0
0438 illegal character 8
0777.77 illegal char .
42
Hexadecimal integer constants
These constants begin with 0x or OX and are followed by combination of digits
taken from hexadecimal digits 0 to 9, a to f or A to F.
Valid Hexadecimal integer constants are:
OX0 OX1 OXF77 Oxabcd.
Invalid Hexadecimal integer constants are:
OBEF x is not included
Ox.4bff illegal char (.)
OXGBC illegal char G
Maximum values these constants can have are as follows:
Integer constants Maximum value
Decimal integer 32767
Octal integer 77777
Hexadecimal integer 7FFF
Unsigned interger constants: Exceed the ordinary integer by magnitude of 2,
they are not negative. A character U or u is prefixed to number to make it
unsigned.
Long Integer constants: These are used to exceed the magnitude of ordinary
integers and are appended by L.
For example,
50000U decimal unsigned.
1234567889L decimal long.
0123456L otal long.
0777777U otal unsigned.
Floating Point Constants
What is a base 10 number containing decimal point or an exponent.
Examples of valid floating point numbers are:
0. 1.
000.2 5.61123456
50000.1 0.000741
1.6667E+3 0.006e-3
Examples of Invalid Floating Point numbers are:
1 decimal or exponent required.
1,00.0 comma not allowed.
2E+10.2 exponent is written after integer quantity.
3E 10 no blank space.
A Floating Point number taking the value of 5 x 104 can be represented as:
5000. 5e4
5e+4 5E4
5.0e+4 .5e5
43
The magnitude of floating point numbers range from 3.4E –38 to a maximum of
3.4E+38, through 0.0. They are taken as double precision numbers. Floating Point
constants occupy 2 words = 8 bytes.
Character Constants
This constant is a single character enclosed in apostrophes ‘ ’ .
For example, some of the character constants are shown below:
‘A’, ‘x’, ‘3’, ‘$’
‘\0’ is a null character having value zero.
Character constants have integer values associated depending on the character set
adopted for the computer. ASCII character set is in use which uses 7-bit code with 27
= 128 different characters. The digits 0-9 are having ASCII value of 48-56 and ‘A’
have ASCII value from 65 and ‘a’ having value 97 are sequentially ordered. For
example,
‘A’ has 65, blank has 32
ESCAPE SEQUENCE
There are some non-printable characters that can be printed by preceding them with ‘\’
backslash character. Within character constants and string literals, you can write a
variety of escape sequences. Each escape sequence determines the code value for a
single character. You can use escape sequences to represent character codes:
- you cannot otherwise write (such as \n)
- that can be difficult to read properly (such as \t)
- that might change value in different target character sets (such as \a)
- that must not change in value among different target environments (such as \0)
The following is the list of the escape sequences:
Character Escape Sequence
” \”
‘ \’
? \?
\ \\
BEL \a
BS \b
FF \f
NL \n
CR \r
HT \t
VT \v
String Constants
It consists of sequence of characters enclosed within double quotes. For example,
“ red ” “ Blue Sea ” “ 41213*(I+3) ”.