Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Other

Structures and file management in c language

Rating
-
Sold
-
Pages
35
Uploaded on
15-03-2023
Written in
2022/2023

these notes comprises of complete introduction to c language with proper follow up examples which creates a strong base for further deep study of other languages like c++ ,python etc.

Institution
Course

Content preview

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 char name[10];
member1; int usn;
datatype float marks;
member2; };
datatype
member3;
}
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;
}; };

Written for

Institution
Course

Document information

Uploaded on
March 15, 2023
Number of pages
35
Written in
2022/2023
Type
OTHER
Person
Unknown

Subjects

$8.59
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
hemantkumar1

Get to know the seller

Seller avatar
hemantkumar1 nmit
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
7
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions