Datatypes
C language supports rich set of data types. Storage
representations and machine instructions to handle constants
differ from machine to machine. The variety of data types
available allow the programmer to select appropriate to the
needs to the applications as well ANSI C supports four types of
data types:
- primary data types
- User defined data types
- Derived data types
- Empty dataset.
Primary data types:
An atomic data is a fundamental
unit of information which cannot be broken down to constituent
parts .All C compilers support four fundamental data types . The
type of data type supported in language dictates the type of
values which can be processed by the language.
The four main
data types are:
- int (it holds an integer value)
- float (floating point)
- char (character)
- double (double precision floating point values)
INTEGER DATATYPES:
Integers are whole numbers with a
range of values supported by the machine. Generally the integer
occupy one word of storage and since the word sizes of machines
vary (typically 16 or 32 bits) the size of the integer that can
be stored depends on the computer .However we have two types
here
- signed
- unsigned
A signed integer uses one bit for the sign and 15
bits for the magnitude of the given number whereas in the case
of unsigned integer it only uses all the 16 bits for the
magnitude of the number ignoring the sign.
C has three
classes of integer storage, namely short int, int, long int in
both signed and unsigned cases
Usually the range of the
unsigned numbers will be from 0 to 65535
Short int
represents fairly a small integer values and requires half the
amount of storage as a regular int number uses. We declare long
and unsigned values to increase the range of number values. The
use of qualifier “signed” on integers is optional because by
default declaration assumes as a signed number.
FLOATING POINT TYPES:
Floating point or real numbers are stored in 32 bits ,with 6
digits of precision .floating point numbers are defined by the
key word “float “. When the accuracy provided by a float number
is not sufficient then , the double can be used to define a
number .A double data type uses 64 bits with a precision of 14
digits. These are known as double precision numbers.
Note:
Double and float represent same data type but double differs
from float in precision of digits.
Character types:
A single character can be defined as character data type .Characters are usually stored in 8 bits of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 to 255.signed chars have values from -128 to 127.
DERIVED DATATYPES:
The data types that are derived from primary data types .These are derived from one or more simple data types. Data types such as arrays, structures, unions, pointers come under category.
USER DEFINED DATA TYPES:
In addition to these data types there exists some data types
that user can define in a program for simpler use ,such data
types are called user defined data types .
There are two
different types of user defined data types. They are
- typedef
- enum
TYPEDEF:
C supports a feature called “type
definition” that 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 identifier;
Where type
refers to the 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.
We should note that the new type is only new in name but not in
data type .The typedef cannot create a new type.
Examples:
typedef int units;
typedef float marks;
Here, the
units represent the int and the marks represent the float .They
can be later be used to declare variables as
units batch1,
batch2;
marks name1 [15], name2[15];
Here the unit
symbolizes int and marks refer to float .batch1 and batch2 are
declared as int variable and name1 [15], name2 [15] are declared
as 15 floating point array values. The main advantage of typedef
is that we can create meaningful data type names for increasing
the readability of the program .
ENUM: Enum is
another user defined data type called enumerated data type .It
is defined as follows:
enum identifier {value1, value2,
value3, ……………, value n};
Where the identifier is a user
defined enumerated data type which can be used to declare
variables that can have one of the values enclosed within the
braces (known as enumeration constants). After this definition,
we can declare the variables to be of this ‘new’ type as below:
enum identifier v1,v2,v3………., vn;
Example:
enum day {Monday, Tuesday, Wednesday, Friday, Saturday, Sunday
};
enum day week_st , week_end;
week_st=Monday;
week_end=Friday;
if (week_st==Tuesday)
week_end=Saturday;
The C compiler automatically assigns
integer digits beginning with 0 to all the enumeration
constants. That is, the enumeration constant value1 is assigned
0,value2 is assigned 1 and so on. However the automatic
assignments can be over ridden by assigning values explicitly to
the enumeration constants.
The declaration and
definition of enumerated variables can be combined in one
statement
Example:
enum day{Monday,
Tuesday,…….., Sunday}week_st, week_end;