Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

C PROGRAMMING

FOR PROBLEM SOLVING


STRUCTURES

Structures is a collection of elements of different data type, grouped under a


single name.
• Suppose, you want to store the information about person about his name,
citizenship number and salary.
• These information done separately but, better approach will be collection
of these information under single name because all these information are
related to person.
• For this , structure can be used
DECLARATION OF STRUCTURES
• The keyword struct is used to declare structure followed by structure tag.
• The syntax is shown below:

• The variables that are used to store the data are called members of the
structure.
• Structure definition is terminated with a semicolon.
• We can create the structure for a person as shown below:
DECLARATION OF STRUCTURE VARIABLES

• The structure variables can be declared using 2 methods:


– Tagged Structure:
when a structure is defined, it creates a user-defined type but, no
storage is allocated. The syntax for creating structure variable can be
written as:
• Example:
struct student
{
char name[50];
char usn[10];
int age;
float marks;
}s1,s2;
Type Defined Structure

• Another way of creating structure variable is by using the keyword


typedef:

• Here the above statement declares that the variables s1 and s2 are variables
of type STUDENT(which is of type as struct student).
• Typedef existing datatype new_data_type
• Typedef int INTERGER;
Accessing Members of a Structure

• The members of a structure can be accessed by using(.) dot operator.


• Any member of a structure can be accessed as:

• Structure variable Initialization:


– Initializing a structure means assigning some constant to the members
of structure. The syntax for initializing the structure member variables
is:

Example:
struct student s1={“CANARA”,”4CB”,18,25.0};
• Reading and writing structure variable:
Based on the member type structure members can use input and output
statements for reading writing.
printf(“\n the student details are”);
printf(“\n Name: %s”,s.name);
printf(“\nUSN: %s”,s.usn);
printf(“\n age:%d”,s.age);
printf(“\n marks: %f”, s.marks);
}
STRUCTURES WITHIN STRUCTURES (NESTED STRUCTURES):
A structure within a structure is called nested structure i.e. a structure is a
member of another structure.
We can create nested structures in two ways
1. Placing complete definition of a structure inside the definition of another
structure.
2. Structure is defined separately and a variable of structure can be placed
inside the definition of another structure definition.
• ACCESSING NESTED STRUCTURE MEMBERS:
– SYNTAX:

– EXAMPLE:
S.dob.day=20;
S.dob.month=3;
S.dob.year=2018;
• Array of structures:
An array of structure is declared in the same way as we had declared
array of built in data type.
Syntax:
Passing individual member
#include<stdio.h>
typedef struct
{
int x;
int y;
}POINT;
Void display(int a,int b);
Main()
{
POINT p1={2,3};
display(p1.x,p1.y);
}
Void display(int a,int b)
{
printf(“the coordinates of the pointare:%d %d”,a,b);
Passing the entire structure

#include<stdio.h>
Typedef struct
{
int x;
int y;
}POINT;
Void display(POINT);
Main()
{
POINT p1={2,3};
display(p1);
}
Void display(POINT p)
{
Printf(“%d%d”,p.x,p.y);
}
union
Union is collection of variables of different data type, you can
store information in one field at any one time.

Declaring a union
union union_name
{
data_type var_name;
data_type var_name;
};
#include<stdio.h>
typedef Union point1
{
Int x;
Int y;
};
Mian()
{
point p1;
P1.x=4;
P1.y=5;
}
Struct student
{
union
{
Char name[20];
Int roll;
};
Int marks;
};
Main()
{
Struct student stud;
Enum enumaration_name{identifier1, identifier2….};
Enum colour{red=2,blue,black=7};
Stdin- resd operation
Stdout- write operation
Stderr- write operation
Types of file
1. Text
2. Binar
Uses of files
steps
3. Declare a file pointer variable FILE *file_pointer_name; FILE *fp;
4. Open the file FILE *fopen(const char *filename, const chat *mode)
5. Fp=fopen(“student.DAT”,”r+”);
6. Process the file
7. Close the file fclose()
8. C==‘\0’
9. C==EOF

You might also like