Structures And File Management
Introduction
Derived data type : int,float,char,double, are the primitive data types .
Using these primitive data types we can derive other data types and such data types
derived from basic types are called derived data types.
Type definition
Typedef is a keyword that allows the programmer to create new data type name for an
already existing datatype.
Syntax:
typedef old datatype newdatype ;
Example: typedef int MARKS;
typedef float AMOUNT;
Example:
Program to compute simple interest using typedef definition
#include<stdio.h>
typedef float AMOUNT;
typedef float TIME;
typedef float RATE;
void main( )
{
AMOUNT p,si;
TIME t;
RATE r;
printf(“enter the value of p,t,r\n”);
scanf(“%f%f%f”,&p,&t,&r);
si=(p*t*r)/100;
printf(“si=%f\n”,si);
}
Advantages of typede:
1. Provide a meaningful way of declaring variable
2. Increases readability of program
3. A complex and lengthy declaration can be reduced to short and meaningful declaration
4. Helps the programmer to understand source code easily.
, Structures
Definition: A structure is defined as a collection of variables of same data type or
dissimilar datatype grouped together under a single name.
Syntax Example
struct tagname struct student
{ {
datatype member1; char name[10];
datatype member2; int usn;
datatype member3; float marks;
} };
where
struct is a keyword which informs the compiler that a structure is being defined
tagname: name of the structure
member1,member2: members of structure:
type1,type 2 : int,float,char,double
Structure Declaration
As variables are declared ,structure are also declared before they are used:
Three ways of declaring structure are:
Tagged structure
Structure without tag
Type defined structures
1.Tagged structure
syntax Example
struct tag_name struct student
{ {
data type member1; char name[20];
data type member2; int usn;
-------------------- float marks;
-------------------- };
};
Declaration of structure variables
struct tagname v1,v2,v3…vn; struct student s1,s2,s3;
, 2.structure without tagname
syntax Example
struct struct
{ {
data type member1; char name[20];
data type member2; int usn;
; float marks;
-------------------- }s1,s2;
--------------------
}v1,v2,v3;
3.Type defined structure
syntax Example
typedef struct typedef struct
{ {
data type member1; char name[20];
data type member2; int usn;
-------------------- float marks;
-------------------- }STUDENT;
}TYPE_ID;
Declaring structure variables
TYPE_ID v1,v2,v3…vn; STUDENT s1,s2,s3;
Memory Allocation for structure variable s1:
name(10 bytes) usn(2 bytes) marks (4 bytes)
memory allocated for a structure variable s1=memory allotted for name+usn+marks
10+2+4
16 bytes.
Structure initialization
Syntax:
struct tagname variable={v1,v2….vn};
, example
struct student s1={“sony”,123,24};
Accessing structures
The members of a structure can be accessed by specifying the variable followed by dot
operator followed by the name of the member.
For example,
consider the structure definition and initialization along with
memory representation as shown below:
struct student
{
char name[20];
int usn;
float marks;
} s1;
struct student s1 = {"aditi",002,40};
By specifying
Variblename . membername
Example
S1.name
S1.usn
S1.marks
Structure operations
1. Copying of structure variables
2. Arithmetic operations on structures
3. Comparision of two structures
1. Copying of structure variables
copying of two structure variables is achieved using assignment operator.
Consider two structure definition of student and emplpoyee
struct student struct employee
{ {
char name[20]; char ename[20];
int usn; int eid;
float marks; float salary;
}; };
Introduction
Derived data type : int,float,char,double, are the primitive data types .
Using these primitive data types we can derive other data types and such data types
derived from basic types are called derived data types.
Type definition
Typedef is a keyword that allows the programmer to create new data type name for an
already existing datatype.
Syntax:
typedef old datatype newdatype ;
Example: typedef int MARKS;
typedef float AMOUNT;
Example:
Program to compute simple interest using typedef definition
#include<stdio.h>
typedef float AMOUNT;
typedef float TIME;
typedef float RATE;
void main( )
{
AMOUNT p,si;
TIME t;
RATE r;
printf(“enter the value of p,t,r\n”);
scanf(“%f%f%f”,&p,&t,&r);
si=(p*t*r)/100;
printf(“si=%f\n”,si);
}
Advantages of typede:
1. Provide a meaningful way of declaring variable
2. Increases readability of program
3. A complex and lengthy declaration can be reduced to short and meaningful declaration
4. Helps the programmer to understand source code easily.
, Structures
Definition: A structure is defined as a collection of variables of same data type or
dissimilar datatype grouped together under a single name.
Syntax Example
struct tagname struct student
{ {
datatype member1; char name[10];
datatype member2; int usn;
datatype member3; float marks;
} };
where
struct is a keyword which informs the compiler that a structure is being defined
tagname: name of the structure
member1,member2: members of structure:
type1,type 2 : int,float,char,double
Structure Declaration
As variables are declared ,structure are also declared before they are used:
Three ways of declaring structure are:
Tagged structure
Structure without tag
Type defined structures
1.Tagged structure
syntax Example
struct tag_name struct student
{ {
data type member1; char name[20];
data type member2; int usn;
-------------------- float marks;
-------------------- };
};
Declaration of structure variables
struct tagname v1,v2,v3…vn; struct student s1,s2,s3;
, 2.structure without tagname
syntax Example
struct struct
{ {
data type member1; char name[20];
data type member2; int usn;
; float marks;
-------------------- }s1,s2;
--------------------
}v1,v2,v3;
3.Type defined structure
syntax Example
typedef struct typedef struct
{ {
data type member1; char name[20];
data type member2; int usn;
-------------------- float marks;
-------------------- }STUDENT;
}TYPE_ID;
Declaring structure variables
TYPE_ID v1,v2,v3…vn; STUDENT s1,s2,s3;
Memory Allocation for structure variable s1:
name(10 bytes) usn(2 bytes) marks (4 bytes)
memory allocated for a structure variable s1=memory allotted for name+usn+marks
10+2+4
16 bytes.
Structure initialization
Syntax:
struct tagname variable={v1,v2….vn};
, example
struct student s1={“sony”,123,24};
Accessing structures
The members of a structure can be accessed by specifying the variable followed by dot
operator followed by the name of the member.
For example,
consider the structure definition and initialization along with
memory representation as shown below:
struct student
{
char name[20];
int usn;
float marks;
} s1;
struct student s1 = {"aditi",002,40};
By specifying
Variblename . membername
Example
S1.name
S1.usn
S1.marks
Structure operations
1. Copying of structure variables
2. Arithmetic operations on structures
3. Comparision of two structures
1. Copying of structure variables
copying of two structure variables is achieved using assignment operator.
Consider two structure definition of student and emplpoyee
struct student struct employee
{ {
char name[20]; char ename[20];
int usn; int eid;
float marks; float salary;
}; };