The data type is a declaration for memory locations or variables that determines the characteristics of the data that may be stored and the methods (operations) of processing that are permitted involving them. C programming is rich in data types. ANSI C supports three classes of data types.
I) Primary (Fundamental) data type
The data type that is already defined in C. All C compiler supports integer (int), character (char), single precision floating point (float), double precision floating point(double), and void data type. Based on the size of the data type we can calculate the range of the data that it can hold.
Let us take an example of an unsigned character in a 16-bit machine. A character has a size of 8 bits. Since 8 bit in a binary format, we have the possibility of number range 00000000 to 11111111. This range is based on a binary number system, which can be defined by the range 0 to 2n-1, where n is the number of bits in that number. Hence the range is 0 to 255.
Let’s say the number is signed, meaning that the first bit is used for sign, 0 indicating positive and 1 indicating negative. So, now we have only seven bits to define the range. Our first bit can represent +ve or -ve. The range of the other seven bits is 0 to 27-1. So the final range is -(27) to +(27-1). i.e. -128 to 127.
The following tables show the details about keywords, size, and range of the different primary data types(for the 16-bit machines/compilers).
Data Type | Keyword | Size(bits) | Range |
---|---|---|---|
Signed c=Character | char, signed char | 8 | -128 to 127 (-27 to 27 – 1) |
Unsigned Character | unsigned char | 8 | 0 to 255 (0 to 28 -1) |
Signed Integer | int, signed int | 16 | -32768 to 32767 (-215 to 215 – 1) |
Unsigned Integer | unsigned int | 16 | 0 to 65535 (0 to 216 -1) |
Signed Short Integer | short int, signed short int | 8 | -128 to 127 |
Unsigned Short Integer | unsigned short int | 8 | 0 to 255 |
Long Integer | long int | 32 | (-231 to 231 – 1) |
Unsigned Long Integer | unsigned long int | 32 | (0 to 232 -1) |
Single Precision Floating | float | 32 | 3.4E-38 to 3.4E+38 |
Double Precision Floating | double | 64 | 1.7E-308 to 1.7E+308 |
Extended Double | long double | 80 | 3.4E-4932 to 1.1E+4932 |
II) Derived Data Type
Data types that are derived from primary data types are Derived data types. It does not create a new data type, but rather adds various functionalities to the existing data types. Arrays, Pointers, structure, and union are derived data types in C.
III) User-defined Data Type
C supports the feature known as “Type definition” which allows the user to define the identifier that would represent an existing data type. It creates an alias for an existing data type.
For example:
typedef unsigned long int uli;
Now the new data of type unsigned long int can be declared as:
uli x;
Another user-defined data type is the enumerated data type provided by the ANSI C. (enum)