Structure
{C}
Programming
Programming for Problem Solving (PPS)
GTU # 3110003
USING
Prof. Nilesh Gambhava
�Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot
Data Types
Data types in C
Primary Data type
(int, float, char)
Secondary Data type
Derived Data type
(array, pointer)
User defined Data type
(structure, union, enum)
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
2
User Defined Datatype
Author: Yashavant Kanetkar Datatype: char / string
Page: 320 Datatype: int
Price: 255.00 Datatype: float
Roll_No: 180540107001 Datatype: int
CPI: 7.46 Datatype: float
Backlog: 01 Datatype: int
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
3
What is Structure?
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
4
Syntax to Define Structure
Structure_name is name of custom type
memberN_declaration is individual member
struct structure_name
{
member1_declaration;
member2_declaration;
. . .
memberN_declaration;
};
1
2
3
4
5
6
7
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
5
Example to Define Structure
struct student
{
char name[30]; // Student Name
int roll_no; // Student Roll No
float CPI; // Student CPI
int backlog; // Student Backlog
};
1
2
3
4
5
6
7
struct student
{
char name[30] = “ABC”; // Student Name
. . .
};
1
2
3
4
5
Example
Example
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
6
Create Structure variable
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
7
Create Structure Variable – Cont.
struct structure_name
{
member1_declaration;
member2_declaration;
. . .
memberN_declaration;
} structure_variable;
1
2
3
4
5
6
7
struct student
{
char name[30]; // Student Name
int roll_no; // Student Roll No
float CPI; // Student CPI
int backlog; // Student Backlog
} student1;
1
2
3
4
5
6
7
Example
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
8
Create Structure Variable – Cont.
struct structure_name structure_variable;
1
struct student
{
char name[30]; // Student Name
int roll_no; // Student Roll No
float CPI; // Student CPI
int backlog; // Student Backlog
};
struct student student1; // Declare structure variable
1
2
3
4
5
6
7
8
Example
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
9
Access Structure member (data)
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
10
Access Structure member (data) – Cont.
structure_variable.member_name;
1
// Assign CPI of student1
student1.CPI = 7.46;
1
2
pointer_to_structure->member_name;
1
// Student1 is a pointer to student type
student1 -> CPI = 7.46;
1
2
Syntax
Syntax
Example
Example
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
11
WAP to print Odd numbers between 1 to n
#include <stdio.h>
struct student
{
char name[40]; // Student name
int roll; // Student enrollment
float CPI; // Student mobile number
int backlog;
};�int main()
{
struct student student1; // Simple structure variable
// Input data in structure members using dot operator
printf("Enter Student Name:");
scanf("%s", student1.name);
printf("Enter Student Roll Number:");
scanf("%d", &student1.roll);
printf("Enter Student CPI:");
scanf("%f", &student1.CPI);
printf("Enter Student Backlog:");
scanf("%d", &student1.backlog);
// Display data in structure members using dot operator
printf("\nStudent using simple structure variable.\n");
printf("Student name: %s\n", student1.name);
printf("Student Enrollment: %d\n", student1.roll);
printf("Student CPI: %f\n", student1.CPI);
printf("Student Backlog: %i\n", student1.backlog);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Enter Student Name:aaa
Enter Student Roll Number:111
Enter Student CPI:7.89
Enter Student Backlog:0
Student using simple structure variable.
Student name: aaa
Student Enrollment: 111
Student CPI: 7.890000
Student Backlog: 0
Program
Output
Write a program to read and display student information using structure.
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
12
WAP to print Odd numbers between 1 to n
#include<stdio.h>
struct time {
int hours;
int minutes;
int seconds;
};�int main() {
struct time t1,t2;
int h, m, s;
//1st time
printf ("Enter 1st time.");
printf ("\nEnter Hours: ");
scanf ("%d",&t1.hours);
printf ("Enter Minutes: ");
scanf ("%d",&t1.minutes);
printf ("Enter Seconds: ");
scanf ("%d",&t1.seconds);
printf ("The Time is %d:%d:%d",t1.hours,t1.minutes,t1.seconds);
//2nd time
printf ("\n\nEnter the 2nd time.");
printf ("\nEnter Hours: ");
scanf ("%d",&t2.hours);
printf ("Enter Minutes: ");
scanf ("%d",&t2.minutes);
printf ("Enter Seconds: ");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Enter 1st time.
Enter Hours: 1
Enter Minutes: 20
Enter Seconds: 20
The Time is 1:20:20
Enter the 2nd time.
Enter Hours: 2
Enter Minutes: 10
Enter Seconds: 10
The Time is 2:10:10
Sum of the two time's is 3:30:30
Program
Output
Write a program to declare time structure and read two different time period and display sum of it.
scanf ("%d",&t2.seconds);
printf ("The Time is %d:%d:%d",t2.hours,t2.minutes,t2.seconds);
h = t1.hours + t2.hours;
m = t1.minutes + t2.minutes;
s = t1.seconds + t2.seconds;
printf ("\nSum of the two time's is %d:%d:%d",h,m,s);
return 0;
}
27
28
29
30
31
32
33
34
35
36
37
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
13
Array of Structure
struct structure_name
{
member1_declaration;
member2_declaration;
...
memberN_declaration;
} structure_variable[size];
1
2
3
4
5
6
7
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
14
WAP to print Odd numbers between 1 to n
#include<stdio.h>
struct student {
char name[20];
int rollno;
float cpi;
};
int main( ) {
int i,n;
printf("Enter how many records u want to store : ");
scanf("%d",&n);
struct student sarr[n];
for(i=0; i<n; i++)
{
printf("\nEnter %d record : \n",i+1);
printf("Enter Name : ");
scanf("%s",sarr[i].name);
printf("Enter RollNo. : ");
scanf("%d",&sarr[i].rollno);
printf("Enter CPI : ");
scanf("%f",&sarr[i].cpi);
}
printf("\n\tName\tRollNo\tMarks\t\n");
for(i=0; i<n; i++) {
printf("\t%s\t\t%d\t\t%.2f\t\n", sarr[i].name, sarr[i].rollno, sarr[i].cpi);
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Enter how many records u want to store : 3
Enter 1 record :
Enter Name : aaa
Enter RollNo. : 111
Enter CPI : 7.89
Enter 2 record :
Enter Name : bbb
Enter RollNo. : 222
Enter CPI : 7.85
Enter 3 record :
Enter Name : ccc
Enter RollNo. : 333
Enter CPI : 8.56
Name RollNo Marks
aaa 111 7.89
bbb 222 7.85
ccc 333 8.56
Program
Output
Write a program to read and display N student information using array of structure.
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
15
Structure using Pointer
#include <stdio.h>
struct student {
char name[20];
int rollno;
float cpi;
};
int main()
{
struct student *studPtr, stud1;
studPtr = &stud1;
printf("Enter Name: ");
scanf("%s", studPtr->name);
printf("Enter RollNo: ");
scanf("%d", &studPtr->rollno);
printf("Enter CPI: ");
scanf("%f", &studPtr->cpi);
printf("\nStudent Details:\n");
printf("Name: %s\n", studPtr->name);
printf("RollNo: %d", studPtr->rollno);
printf(”\nCPI: %f", studPtr->cpi);
return 0;
}
Enter Name: ABC
Enter RollNo: 121
Enter CPI: 7.46
Student Details:
Name: ABC
RollNo: 121
CPI: 7.460000
Program
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
16
Nested Structure
struct structure_name1
{
member1_declaration;
member2_declaration;
...
memberN_declaration;
};�struct structure_name2
{
member1_declaration;
member2_declaration;
...
struct structure1 obj;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
17
#include<stdio.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};�struct Student
{
char name[25];
int roll;
float cpi;
struct Address Add;
};�int main()
{
int i;
struct Student s;
printf("\n\tEnter Student Name : ");
scanf("%s",s.name);
printf("\n\tEnter Student Roll Number : ");
scanf("%d",&s.roll);
printf("\n\tEnter Student CPI : ");
scanf("%f",&s.cpi);
printf("\n\tEnter Student House No : ");
scanf("%s",s.Add.HouseNo);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Details of Students
Student Name : aaa
Student Roll Number : 111
Student CPI : 7.890000
Student House No : 39
Student City : rajkot
Student Pincode : 360001
Program
Output
Write a program to read and display student information using nested of structure.
printf("\n\tEnter Student City : ");
scanf("%s",s.Add.City);
printf("\n\tEnter Student Pincode : ");
scanf("%s",s.Add.PinCode);
printf("\nDetails of Students");
printf("\n\tStudent Name : %s",s.name);
printf("\n\tStudent Roll Number : %d",s.roll);
printf("\n\tStudent CPI : %f",s.cpi);
printf("\n\tStudent House No : %s",s.Add.HouseNo);
printf("\n\tStudent City : %s",s.Add.City);
printf("\n\tStudent Pincode : %s",s.Add.PinCode);
return 0;
}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Prof. Nilesh Gambhava
#3110003 (PPS) – Structure
18
Thank you
✓