Data types are those by which we declare the type of variable, constants,
function etc. and which allow the programmer to select the type appropriate to
the needs of the application as well as the machine.
Following is the chart of data types available in C.
1. INTERNAL DATA
TYPE
·
Standard data types
o Simple data types
§ char (with signed
and unsigned)
§ short int, int, long
int, (also with signed or unsigned)
§ float, double, long
double
o Structure data type
§ Array, Structure,
Union
·
Dynamic data types (pointers)
·
User defined data type (typedef and enum)
2. EXTERNAL DATA
TYPE (file pointer)
|
Ø
Internal data type is that data type for
which the space is allocated in virtual
main memory.
Ø
External data type is that data type for
which the space is allocated in non-virtual
secondary memory.
o
Static data type is that data type for
which the allocation information is fixed before the run time. During execution
it cannot be changed.
o
Dynamic data type is that data type for
which the allocation information is decided in during execution. So it can be
changed during runtime.
o
User Defined data type
– There are two
user defined data types first is known as “type definition” and second is
“enumerated” data type.
Standard data types -
Ø Simple data
types or Primary data types – All C compilers support three fundamental
data types namely:
1. Character (char)
2. Integer (int)
3. Floating point (float)
1. Character types – A single
character can be defined as a character (char)
type data. Characters are usually stored in 8 bit ( 1 byte) of internal
storage. The qualifier signed and unsigned may be explicitly applied to char.
Character Type
|
||||
SN
|
Type
|
Format
|
Size (bits)
|
Range
|
1.
|
char / signed char
|
%c
|
8
|
-128 to 127
|
2.
|
unsigned char
|
%c
|
8
|
0 to 255
|
2. Integer type – Integers are
whole numbers with a range of value supported by a particular machine.
Generally integers occupy one word of storage, and since the word size of
machines vary the size of an integer that can be stored depend on the computer.
In order to provide some control over the range of numbers and storage
space, C has three classes of integer storage namely:
1. short
int,
2. int,
3. long
int (All these in both form signed and unsigned form)
short int – it represents fairly
amall integer value and requires half the amount of storage a regular integer
number.
signed/unsigned int – unlike signed int,
unsigned integers use all the bits for the magnitude of the number and are
always positive. Therefore for a 16 bit machine, the range of unsigned integer
number will be 0 to 65,535.
long and unsigned
integer – we declare long and unsigned integers to increase the range of value.
The use of qualifier signed on integers is optional because the default
declaration assume a signed number.
Integer Type (on
16-bit machine)
|
||||
SN
|
Type
|
Format
|
Size (bits)
|
Range
|
1.
|
short int / signed short int
|
%d
|
8
|
-128 to 127
|
2.
|
unsigned short int
|
%u
|
8
|
0 to 255
|
3.
|
int / signed int
|
%d
|
16
|
-32768 to 32767
|
4.
|
unsigned int
|
%u
|
16
|
0 to 65,535
|
5.
|
long int / signed long int
|
%ld
|
32
|
-2,147,483,648 to
2,147,483,647
|
6.
|
unsigned long
|
%lu
|
32
|
0 to 2,294,967,295
|
3. Floating Point Type – Floating point
numbers are defined in C by the keyword float.
■ double type represents the same data types that float represents, but with a greater
precision. To extent the precision further, we may use long double which uses 80 bits.
Note: Other remaining data
types such as (Structured, Dynamic and External) will discussed later in
successive chapters.
Floating Point Type
|
||||
SN
|
Type
|
Format
|
Size (bits)
|
Range
|
1.
|
float
|
%f
|
32
|
3.4 e-38 to 3.4 e+38
|
2.
|
double
|
%lf
|
64
|
1.7 e-308 to 1.7 e+308
|
3.
|
long double
|
%Lf
|
80
|
3.4 e-4932 to 1.1 e+4932
|
The summary of simple data types:
Data types Size
(bits) Range
-------------------------------------------------------------------------------------------
Char
or Signed Char 8 -128 to 127
Unsigned
Char 8 0 to 255
Int
or Signed int 16 -32768
to 32767
Unsigned
int 16 0 to 65535
Unsigned
or
Signed
short int 8 -128 to 127
Unsigned
short int 8 0 to
255
Unsigned
or
signed
long int 32 -2147483648
to 2147483647
Unsigned
long int 32 0 to 4294967295
Float 32 3.4 e-38 to 3.4
e+38
Double 64 1.7e-308 to 1.7e+308
Long
Double 80 3.4 e-4932 to 3.4 e+4932
Structure data type (Array, Structure, Union)
We will study these
data types in separate chapters of Array, Structure and Union.
Dynamic data types (pointers)
We will study this
data type in separate chapters of Pointers
User defined data types (typedef and
enum)
(a)
Type definition – This allows user to
define an identifier that would represent an existing data type. The user
defined data type identifier can later be used to declare variables. It takes
the general form:
typedef type
idendirier;
where “type” refers to an existing data type and
“identifier” refers to the “new” name given to the data type. The existing data
type may belong to any class of type, including the user-defined ones.
Ex.
typedef int units;
typedef float marks;
Now we can declare variables as:
unit batch1, batch2;
marks name1[50], name2[50];
The main advantage of of typedef is that we can create meaningful datqa type
names for increasing the readability of the program.
(b)
Enumerated – this data type can be
used to declare variables that can have one of the values enclosed with the
braces (known as enumeration constants). After this definition, we can declare
variables to be of this ‘new’ type as:
enum
identifier v1, v2, … vn;
The enumerated variables v1, v2, …vn can only
have one of the values value1, value2, …
valuen. The assignments of the following types are valid:
v1= value3;
v2=value1;
Ex.
enum day { Monday, Tuesday, Wednesday, Thursday,
Friday, Saturday, Sunday }
enum day weenk_st, week_end;
week_st
= Monday;
week_end
= Saturday;
No comments:
Post a Comment