0% found this document useful (0 votes)
29 views21 pages

Unit 4

The document discusses C programming data types including primary, secondary, derived and user defined data types. It explains structures as a user defined data type that allows grouping of different data types under a single name. It provides the syntax to define structures and create structure variables. It also demonstrates how to access members of a structure using dot and arrow operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

Unit 4

The document discusses C programming data types including primary, secondary, derived and user defined data types. It explains structures as a user defined data type that allows grouping of different data types under a single name. It provides the syntax to define structures and create structure variables. It also demonstrates how to access members of a structure using dot and arrow operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

USING

Structure {C}
Programming
Data Types
Data types are defined as the data storage format that a variable can store a data.

Data types in C

Primary Data type


Secondary Data type
(int, float, char)

Derived Data type User defined Data type


(array, pointer) (structure, union, enum)

C language has built-in datatypes like primary and derived data types.
But, still not all real world problems can be solved using those data types.
We need custom datatype for different situation.
User Defined Datatype
? We need combination of various datatypes to understand different entity/object.
? Example-1:
Book Title: Let Us C Datatype: char / string
Author: Yashavant Kanetkar Datatype: char / string
Page: 320 Datatype: int
Price: 255.00 Datatype: float
? Example-2:
Student Name: ABC Datatype: char / string
Roll_No: 180540107001 Datatype: int
CPI: 7.46 Datatype: float
Backlog: 01 Datatype: int
What is Structure?
? Structure is a collection of logically related data items of different datatypes grouped
together under single name.

? Structure is a user defined datatype.

? Structure helps to build a complex datatype which is more meaningful than an array.

? But, an array holds similar datatype record, when structure holds different datatypes
records.
? Two fundamental aspects of Structure:
Declaration of Structure Variable
Accessing of Structure Member
Syntax to Define Structure
? To define a structure, we need to use struct keyword.
? This keyword is reserved word in C language. We can only use it for structure and its
object declaration.
Syntax
1 struct structure_namestructure_name is name of custom type
2 {
3 member1_declaration;
4 member2_declaration;
5 . . . memberN_declaration is individual member
declaration
6 memberN_declaration;
7 };

? Members can be normal variables, pointers, arrays or other structures.


? Member names within the particular structure must be distinct from one another.
Example to Define Structure
Example
1 struct student
2 {
3 char name[30]; // Student Name
4 int roll_no; // Student Roll No
5 float CPI; // Student CPI
6 int backlog; // Student Backlog
7 };

? You must terminate structure definition with semicolon ;.


? You cannot assign value to members inside the structure definition, it will cause
compilation error.
Example
1 struct student
2 {
3 char name[30] = “ABC”; // Student Name
4 . . .
5 };
Create Structure variable
? A data type defines various properties about data stored in memory.

? To use any type we must declare its variable.

? Hence, let us learn how to create our custom structure type objects also known
as structure variable.

? In C programming, there are two ways to declare a structure variable:


1. Along with structure definition
2. After structure definition
Create Structure Variable – Cont.
1. Declaration along with the structure definition
Syntax Example
1 struct structure_name 1 struct student
2 { 2 {
3 member1_declaration; 3 char name[30]; // Student Name
4 member2_declaration; 4 int roll_no; // Student Roll No
5 . . . 5 float CPI; // Student CPI
6 memberN_declaration; 6 int backlog; // Student Backlog
7 } structure_variable; 7 } student1;
Create Structure Variable – Cont.
2. Declaration after Structure definition
Syntax
1 struct structure_name structure_variable;

Example
1 struct student
2 {
3 char name[30]; // Student Name
4 int roll_no; // Student Roll No
5 float CPI; // Student CPI
6 int backlog; // Student Backlog
7 };
8 struct student student1; // Declare structure variable
Access Structure member (data)
? Structure is a complex data type, we cannot assign any value directly to it
using assignment operator.

? We must assign data to individual structure members separately.

? C supports two operators to access structure members, using a structure variable.


1. Dot/period operator (.)
2. Arrow operator (->)
Access Structure member (data) – Cont.
1. Dot/period operator (.)
It is known as member access operator. We use dot operator to access members of simple
structure variable.

Syntax Example
1 structure_variable.member_name; 1 // Assign CPI of student1
2 student1.CPI = 7.46;
1. Arrow operator (->)
In C language it is illegal to access a structure member from a pointer to structure variable using
dot operator.
We use arrow operator to access structure member from pointer to structure.

Syntax Example
1 pointer_to_structure->member_name; 1 // Student1 is a pointer to student type
2 student1 -> CPI = 7.46;
Write a program to read and display student information using structure.

WAP to print Odd numbers between 1 to n


Program Output
1 #include <stdio.h> Enter Student Name:aaa
2 struct student Enter Student Roll Number:111
3 {
4 char name[40]; // Student name Enter Student CPI:7.89
5 int roll; // Student enrollment Enter Student Backlog:0
6 float CPI; // Student mobile number
7 int backlog; Student using simple structure variable.
8 };
9 int main()
Student name: aaa
10 { Student Enrollment: 111
11 struct student student1; // Simple structure variable Student CPI: 7.890000
12 // Input data in structure members using dot operator Student Backlog: 0
13 printf("Enter Student Name:");
14 scanf("%s", student1.name);
15 printf("Enter Student Roll Number:");
16 scanf("%d", &student1.roll);
17 printf("Enter Student CPI:");
18 scanf("%f", &student1.CPI);
19 printf("Enter Student Backlog:");
20 scanf("%d", &student1.backlog);
21 // Display data in structure members using dot operator
22 printf("\nStudent using simple structure variable.\n");
23 printf("Student name: %s\n", student1.name);
24 printf("Student Enrollment: %d\n", student1.roll);
25 printf("Student CPI: %f\n", student1.CPI);
26 printf("Student Backlog: %i\n", student1.backlog);
27 }
Write a program to declare time structure and read two different time period and display sum of it.

WAP to print Odd numbers between 1 to n


Program
1 #include<stdio.h> 27 scanf ("%d",&t2.seconds);
2 struct time { 28 printf ("The Time is
3 int hours; 29 %d:%d:%d",t2.hours,t2.minutes,t2.secon
4 int minutes; 30 ds);
5 int seconds; 31 h = t1.hours + t2.hours;
6 }; 32 m = t1.minutes + t2.minutes;
7 int main() { 33 s = t1.seconds + t2.seconds;
8 struct time t1,t2; 34 printf ("\nSum of the two time's is
9 int h, m, s; 35 %d:%d:%d",h,m,s);
10 //1st time 36 return 0;
11 printf ("Enter 1st time."); 37 }
12 printf ("\nEnter Hours: ");
13 scanf ("%d",&t1.hours);
14 printf ("Enter Minutes: "); Output
15 scanf ("%d",&t1.minutes); Enter 1st time.
16 printf ("Enter Seconds: "); Enter Hours: 1
17 scanf ("%d",&t1.seconds); Enter Minutes: 20
18 printf ("The Time is Enter Seconds: 20
19 %d:%d:%d",t1.hours,t1.minutes,t1.seconds); The Time is 1:20:20
20 //2nd time
21 printf ("\n\nEnter the 2nd time."); Enter the 2nd time.
22 printf ("\nEnter Hours: "); Enter Hours: 2
23 scanf ("%d",&t2.hours); Enter Minutes: 10
24 printf ("Enter Minutes: "); Enter Seconds: 10
25 scanf ("%d",&t2.minutes); The Time is 2:10:10
26 printf ("Enter Seconds: "); Sum of the two time's is 3:30:30
Array of Structure
? It can be defined as the collection of multiple structure variables where each variable
contains information about different entities.
? The array of structures in C are used to store information about multiple entities of
different data types.
Syntax
1 struct structure_name
2 {
3 member1_declaration;
4 member2_declaration;
5 ...
6 memberN_declaration;
7 } structure_variable[size];
Write a program to read and display N student information using array of structure.

WAP to print Odd numbers between 1 to n


Program
1 #include<stdio.h> Output
2 struct student {
3 char name[20]; Enter how many records u want to store : 3
4 int rollno;
5 float cpi; Enter 1 record :
6 }; Enter Name : aaa
7 int main( ) { Enter RollNo. : 111
8 int i,n; Enter CPI : 7.89
9 printf("Enter how many records u want to store : ");
10 scanf("%d",&n); Enter 2 record :
11 struct student sarr[n]; Enter Name : bbb
12 for(i=0; i<n; i++) Enter RollNo. : 222
13 {
Enter CPI : 7.85
14 printf("\nEnter %d record : \n",i+1);
15 printf("Enter Name : ");
16 scanf("%s",sarr[i].name); Enter 3 record :
17 printf("Enter RollNo. : "); Enter Name : ccc
18 scanf("%d",&sarr[i].rollno); Enter RollNo. : 333
19 printf("Enter CPI : "); Enter CPI : 8.56
20 scanf("%f",&sarr[i].cpi);
21 } Name RollNo Marks
22 printf("\n\tName\tRollNo\tMarks\t\n"); aaa 111 7.89
23 for(i=0; i<n; i++) { bbb 222 7.85
24 printf("\t%s\t\t%d\t\t%.2f\t\n", sarr[i].name, ccc 333 8.56
25 sarr[i].rollno, sarr[i].cpi);
26 }
27 return 0;
28 }
Write a program to declare time structure and read two different time period and display sum of it using function.

WAP to print Odd numbers between 1 to n


Program
1 #include<stdio.h> Output
2 struct Time {
3 int hours; Enter Hours: 1
4 int minutes; Enter Minutes: 20
5 int seconds; Enter Seconds: 20
6 };
7 struct Time input(); // function declaration Hours : Minutes : Seconds
8 int main() 1 : 20 : 20
9 {
10 struct Time t;
11 t=input();
12 printf("Hours : Minutes : Seconds\n %d : %d :
13 %d",t.hours,t.minutes,t.seconds);
14 return 0;
15 }
16 struct Time input() // function definition
17 {
18 struct Time tt;
19 printf ("Enter Hours: ");
20 scanf ("%d",&tt.hours);
21 printf ("Enter Minutes: ");
22 scanf ("%d",&tt.minutes);
23 printf ("Enter Seconds: ");
24 scanf ("%d",&tt.seconds);
25 return tt; // return structure variable
26 }
Structure using Pointer
Reference/address of structure object is passed as function argument to the definition of function.
Program Output
1 #include <stdio.h> Enter Name: ABC
2 struct student {
3 char name[20]; Enter RollNo: 121
4 int rollno; Enter CPI: 7.46
5 float cpi;
6 };
7 int main() Student Details:
8 { Name: ABC
9 struct student *studPtr, stud1;
10 studPtr = &stud1; RollNo: 121
11 printf("Enter Name: "); CPI: 7.460000
12 scanf("%s", studPtr->name);
13 printf("Enter RollNo: ");
14 scanf("%d", &studPtr->rollno);
15 printf("Enter CPI: ");
16 scanf("%f", &studPtr->cpi);
17 printf("\nStudent Details:\n");
18 printf("Name: %s\n", studPtr->name);
19 printf("RollNo: %d", studPtr->rollno);
20 printf(”\nCPI: %f", studPtr->cpi);
21 return 0;
22 }
Nested Structure
? When a structure contains another structure, it is called nested structure.
? For example, we have two structures named Address and Student. To make Address nested
to Student, we have to define Address structure before and outside Student structure and
create an object of Address structure inside Student structure.
Syntax
1 struct structure_name1
2 {
3 member1_declaration;
4 member2_declaration;
5 ...
6 memberN_declaration;
7 };
8 struct structure_name2
9 {
10 member1_declaration;
11 member2_declaration;
12 ...
13 struct structure1 obj;
14 };
Write a program to read and display student information using nested of structure.
Program
1 #include<stdio.h> 27 printf("\n\tEnter Student City : ");
2 struct Address 28 scanf("%s",s.Add.City);
3 { 29 printf("\n\tEnter Student Pincode : ");
4 char HouseNo[25]; 30 scanf("%s",s.Add.PinCode);
5 char City[25]; 31 printf("\nDetails of Students");
6 char PinCode[25]; 32 printf("\n\tStudent Name : %s",s.name);
7 }; 33 printf("\n\tStudent Roll Number :
8 struct Student 34 %d",s.roll);
9 { 35 printf("\n\tStudent CPI : %f",s.cpi);
10 char name[25]; 36 printf("\n\tStudent House No :
11 int roll; 37 %s",s.Add.HouseNo);
12 float cpi; 38 printf("\n\tStudent City :
13 struct Address Add; 39 %s",s.Add.City);
14 }; 40 printf("\n\tStudent Pincode :
15 int main() 41 %s",s.Add.PinCode);
16 { 42 return 0;
17 int i; 43 }
18 struct Student s;
19 printf("\n\tEnter Student Name : "); Output
20 scanf("%s",s.name); Details of Students
21 printf("\n\tEnter Student Roll Number : "); Student Name : aaa
22 scanf("%d",&s.roll); Student Roll Number : 111
23 printf("\n\tEnter Student CPI : "); Student CPI : 7.890000
24 scanf("%f",&s.cpi); Student House No : 39
25 printf("\n\tEnter Student House No : "); Student City : rajkot
26 scanf("%s",s.Add.HouseNo); Student Pincode : 360001
Practice Programs
1. Define a structure data type called time_struct containing three member’s integer hours, minutes, second.
Develop a program that would assign values to individual member and display the time in following format :
HH:MM:SS
2. WAP to create structure of book with book title, author name, publication, and price. Read data of n books and
display them.
3. Define a structure Person that would contain person name, date of joining, and salary using this structure to read
this information of 5 people and print the same on screen.
4. Define a structure time_struct containing three member’s integer hour, integer minute and integer second. WAP
that would assign values to the individual number and display the time in the following format: 16: 40: 51.
5. Define a structure cricket that will describe the following information:
Player name
Team name
Batting average
6. Using cricket, declare an array player with 50 elements and WAP to read the information about all the 50
players and print team wise list containing names of players with their batting average.
7. Define a structure student_record to contain name, branch, and total marks obtained. WAP to read data for 10
students in a class and print them.

Thank you

You might also like