0% found this document useful (0 votes)
2 views

08_structure

The document explains the concept of structures in C language, highlighting their definition, syntax, and usage through examples. It covers how to declare and initialize structures, take user input, and manage multiple entries using arrays of structures. Additionally, it discusses nested structures and the passing of structures to functions.

Uploaded by

landeparth93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

08_structure

The document explains the concept of structures in C language, highlighting their definition, syntax, and usage through examples. It covers how to declare and initialize structures, take user input, and manage multiple entries using arrays of structures. Additionally, it discusses nested structures and the passing of structures to functions.

Uploaded by

landeparth93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

-------------------------------------------------------------------------

STRUCTURE IN C LANGUAGE
-------------------------------------------------------------------------

Array: collection of similar or same data type;

int a[5] = {23,43,45,23,12}

Structure: collection of different data type;


Syntax:
struct <tag_name>
{
data_member;
........
.......

};

Example:
Declaration of structure

Struct student
{
Char name[20];
int rollno;
float marks;
};

int main()
{
//initialization of structure

struct student s1;

strcpy(s1.name,”david”);
s1.rollno = 210;
s1.marks = 77.5;

printf(“student name is: %s\n”,s1.name);


printf(“student roll number is : %d\n”,s1.rollno);
printf(“student marks is : %f\n”,s1.marks);

}
Taking input from user or keyboard for details into structure.

Example:
Declaration of structure

Struct student
{
Char name[20];
int rollno;
float marks;
};

int main()
{
//Taking input from user

struct student s1;

printf(“Enter student name is : “);


scanf(“%s”,s1.name);
printf(“Enter student rollno is : “);
scanf(“%d”,&s1.rollno);
printf(“Enter student marks is : “);
scanf(“%f”,&s1.marks);

printf(“student name is: %s\n”,s1.name);


printf(“student roll number is : %d\n”,s1.rollno);
printf(“student marks is : %f\n”,s1.marks);

If users wants to enter more than one details of student than what should
we do?

First method:

Example:
Declaration of structure

Struct student
{
Char name[20];
int rollno;
float marks;
};

int main()
{
//Taking input from user

struct student s1,s2,s3; // creating three object


from structure

printf(“Enter student name is : “);


scanf(“%s”,s1.name);
printf(“Enter student rollno is : “);
scanf(“%d”,&s1.rollno);
printf(“Enter student marks is : “);
scanf(“%f”,&s1.marks);

printf(“Enter student name is : “);


scanf(“%s”,s2.name);
printf(“Enter student rollno is : “);
scanf(“%d”,&s2.rollno);
printf(“Enter student marks is : “);
scanf(“%f”,&s2.marks);

printf(“Enter student name is : “);


scanf(“%s”,s3.name);
printf(“Enter student rollno is : “);
scanf(“%d”,&s3.rollno);
printf(“Enter student marks is : “);
scanf(“%f”,&s3.marks);

printf(“printing all details of students:\n”);

printf(“student name is: %s\n”,s1.name);


printf(“student roll number is : %d\n”,s1.rollno);
printf(“student marks is : %f\n”,s1.marks);

printf(“student name is: %s\n”,s2.name);


printf(“student roll number is : %d\n”,s2.rollno);
printf(“student marks is : %f\n”,s2.marks);

printf(“student name is: %s\n”,s3.name);


printf(“student roll number is : %d\n”,s3.rollno);
printf(“student marks is : %f\n”,s3.marks);

But that’s not good practices for a good programmer or developer?


So how can we reduce of the code?
Here we are using array of structure?

Example:
Declaration of structure

Struct student
{
Char name[20];
int rollno;
float marks;
};

int main()
{
//Taking input from user

struct student s1[5] ;// here array of structure

for(i = 0; i<5; i++)


{

printf(“Enter student name is : “);


scanf(“%s”,s1[i].name);
printf(“Enter student rollno is : “);
scanf(“%d”,&s1[i].rollno);
printf(“Enter student marks is : “);
scanf(“%f”,&s1[i].marks);
}

printf(“printing all details of students:\n”);

for(I = 0; i<5; i++)


{
printf(“student name is: %s\n”,s1[i].name);
printf(“student rollno is : %d\n”,s1[i].rollno);
printf(“student marks is : %f\n”,s1[i].marks);
}

-------------------------------------------------------------------------
Nested structure in c language
-------------------------------------------------------------------------

Syntax:
struct <tag_name>
{
data_member;
.....

struct <tag_name>
{
data_member;
....
...
};
};

Example:
struct manager
{
char mgr_name[20];
int mgr_id;
float mgr_salary;

struct employee
{
int emp_id;
char emp_name[20];
float emp_salary;
}emp;
}mgr;

int main()
{
clrscr();
printf("Enter manager name : ");
scanf("%s",mgr.mgr_name);
printf("Enter manager id : ");
scanf("%d",&mgr.mgr_id);
printf("Enter manager salary:");
scanf("%f",&mgr.mgr_salary);

-------------------------------------------------------------------------
STRUCTURE PASSING FUNCTION
-------------------------------------------------------------------------

Syntax:
struct <tag_name>
{
data_member;
.....
};

return_type func_name(parameter list);

return_type main_fun(void)
{
Statement;
.....
....
}

return_type func_name(parameter_list) {
data_member;
......
.....
}

Example:
struct product
{
char pro_name[20];
int pro_id;
float pro_price;
}pro;

void showData(struct product p1);

void main()
{
clrscr();
printf("Enter product name : ");
scanf("%s",pro.pro_name);
printf("Enter product id : ");
scanf("%d",&pro.pro_id);
printf("Enter product price : ");
scanf("%f",&pro.pro_price);

showData(pro); // function calling

getch();
}
void showData(struct product p1)
{
printf("product name is : %s\n",p1.pro_name);
printf("product id is : %d\n",p1.pro_id);
printf("product price is : %f\n",p1.pro_price);

You might also like