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.
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.
, 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;
}; };
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller hemantkumar1. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.99. You're not tied to anything after your purchase.