Wednesday 27 February 2013

Explain the various data types used in C with suitable examples.

C language has a rich set of data types. Storage representations and machine instruction to handle constants differ from machine to machine. The variety of data types available allow the programmer to select the type appropriate to the needs of the application as well as the machine.
C supports three classes of data types:
1.  Primary or Fundamental data types: This includes char, int, float, and void data types.
2.  Derived data types: This includes pointer and array.
3.  User-defined data types: This includes structure and unions.


Ø    Integer Types:
Integers are whole numbers with a range of values supported by a particular machine. Integers occupy one word of storage, and since the word size of machines vary (16 or 32 bit) the size of the integer that can be stored depends on the computer. If we use a 16 bit word length, the size of the integer value is limited to the range -32768 to +32767 (that is -215  to +215 -1). A signed integer uses one bit for sign and 15 bits for the magnitude of the number. Similarly, a 32 word length can store an integer ranging from -2,147,438,648 to +2,147,483,647.
C has three classes of integer storage. Namely short int, int and long int. in both signed and unsigned forms.

Ø    Character Types:
A single character can be defined as character (char) type data. Characters are usually stored in 8 bits (one byte) of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 and 255, signed char have values from -128 to +127.

Ø    Floating Point Types:
Floating point or real number are stored in 32 bits (on all 16 bit and 32 bit machines), with 6 digits of precision. Floating point numbers are defined in C by the keyword float. When the accuracy provided by a float number is not sufficient, the type double cab be use to define the number. A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision numbers.

Ø    Void Types:
The void types has no values. This is usually used to specify the type of functions. The type of a function is said to be void when it does not return any value to the calling function. It also play the role of a generic type, meaning that it can any of the other standard type.


Following tables shows the Size and Range of Data Types on a 16-bit Machine:


           Type                        Size (bits/bytes)                     Range
char or signed char          8 / 1                                            -128 to 127
unsigned char                   8 / 1                                            0 to 255
int or signed int                 16 / 2                                         -32,768 to +32,767
unsigned int                      16 / 2                                         0 to 65,535
short int or
unsigned short int            8 / 1                                            -128 to 127
signed short int                 8 / 1                                            0 to 255
long int or                                                                              -2,147,483,648 to
signed long int                  32 / 4                                         +2,147,483,647
unsigned long int             32 / 4                                         0 to 4,294,967,295
float                                     64 / 8                                         3.4E - 38 to 3.4E + 38
double                                 64 / 8                                         1.7E-308 to 1.7E+308
long double                        80 / 10                                       3.4E-4932 to 1.1E-4932