A program usually contains differnt types of data elements and need to store the values being used in program.
C-language has different data types for different types of data and can be broadly classified as :
1• Primary Data types
2• Secondary Data types
Primary Data Types
The primary data type is used for representing a single value only .It is also called simple or fundamental data type. As these are used at primary level in the high level language ,so these are called primary data type .Simple data types has some standard data types.
These are subdivided into 4 categories:
1• Integer data type
2• Floating point data type
3• Character data type
4• Void data type
1• Integer Data Types
Generally and integer occupies 2Bytes memory space and its value range Limited to - 32768 to + 32768 .A signed integer use 1 bit for storing sign and rest 15 bits for number.
Range of an unsigned integer is from 0 to 65535 .The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space.
The general syntax is:
int <variable name>;
Like : int num1;
Example: 5,6,100
2• Floating Point Types
These are further divided into 2 types:
(a) Float Data Type:
Storage size 4 bytes
Range 3.4E-38 to 3.4E +38
Precision 6 digits
General Syntax is: float<variable name>;
Like : float num1;
Example: 9.1 , 3.12
(b) Double Data Type:
Storage size 8 bytes
Range 1.7E-308 to 1.7E+308
General Syntax is: double<variable name>;
Like : double num;
Example: 9.125 , 3.1254
3• Character Data Types
Storage size 1 byte for both
signed and unsigned
chars.
Range 0 to 255 for unsigned
chars
-128 to +127 for signed
chars
General Syntax is: char<variable name>;
Like : char ch ='a';
Example: a,h,l
4• Void Data Type
The void type has no values therefore we cannot declare it as variable as we did in case of integer and float.
The void data type is usually used with function to specify its type.
Secondary data types are derived from the primary data type by adding some additional relationship with the various elements of the primary data types .As these data types have different structure depending on the C-coding these are also called structured data type these are also called Derived data type.
These are further subdivided into four categories:
1. Array & String Data Types
2. Pointer Data Type
3. Structure & Union Data Type
4. User Defined Data Types
1• Array & String Data Types
(a). Array Data Type: Array is set of homogeneous data elements. It is a collection of same types of elements.
An array is a group of related data items , which share common name.
General Syntax is:
data type array name [size];
Or
data type array name[subscript1][subscript2];
Example:
int salary[10]; (integer is of 2 bytes.Size 10 integers means 2*10=20 bytes)
int matrix[2][2];
(b) String Data Type: Combination of characters is called string. String data type is used to store only character data.
If the string is stored in variable then such type of variable should be of character type.
General Syntax is:
data type string_name[size];
Example:char city[10];
char city[10][20];
2• Pointer Data Type
A pointer is a memory variable that stores memory address. Pointer can have any name that is legal for other variable and it is declared in the same fashion like other variables but it is always denoted by '*' operator.
General Syntax is:
datatype *pointer_variable;
Example: int *x;
float *f;
3• Structure & Union Data Type
Structure and union data types are also called heterogeneous data types .They are used to store different types of data elements together using common name.
(a) Structure Data Type: A structured data type is a collection of data elements that are referenced under one name, providing the convenient means of keeping related information together.
General Syntax is:
struct structure _tag
{
data-type-1 member-1;
data-type-2 member-2;
.
.
.
data-type-n member-n;
};
Example:
struct student
{
char grade;
int roll_no;
float fees;
double marks;
}st;
Total size=char+int+float+double=
1+2+4+8=15 bytes
(b) Union Data Type: Union is a data type that declares a set of multiple data elements sharing the same name and memory area.
The compiler allocate sufficient memory to hold the largest variable of the union. All the members of union occupy the same memory locations called addresses.
General Syntax is:
union union _tag
{
data-type-1 member-1;
data-type-2 member-2;
.
.
.
data-type-n member-n;
};
Example:
union student
{
char grade;
int roll_no;
float fees;
double marks;
}st;
Total size=8 bytes
4• User Defined Type Declaration
C language supports a feature where user can define an identifier that characterizes an existing data type .This user defined data type identifier can later be used to declare variables .Two types of user defined data types are:
(a) Typedef Data Type: User can define an identifier that represent an existing data type .The user defined data type identifier can later be used to declare variables.
General Syntax is:
typedef <type> <identifier>;
Like: typedef int number;
For example:
"int n1" or "number n1" both statements declaring an integer variable. We have just changed the default keyword "int" to "number" to declare integer variable.
(b) Enumerated Data Type: This is also used for type definition ,that is , it allows the users to define a variable or an identifier which is used for representation of existing data type . In other words ,it provides us a way to define our own data type .
General Syntax is:
enum identifier {v1,v2,v3.....,vn};
Or
enum identifier {v1,v2,v3....,vn}variable;
Here enum is the reserve word and v1, v 2, v3 ...., vn all are the values which is called enumeration constants.
Example:
enum month {jan,feb,march};
Or
enum year {1999,2000,2001}y;
next
Comments
Post a Comment