SlideShare a Scribd company logo
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 1
Handout#10
Assignment/Program Statement:
Write a C program using structure – to create a structure to read and display
students’ records with given members of structure: Roll Number, name and marks.
Learning Objectives:
Students will be able to
- explain the structure concept
- differentiate between array and structure
- define the structure and declare the structure variables
- read and display records using structure
- write the program using structure
Theory:
 Arrays allow defining type of variables that can hold several data items of
the same kind. Similarly structure is another user defined data type that
allows combining data items of different kinds.
 Structures are used to represent a record. Suppose you want to keep track of
your books in a library. You might want to track the following attributes
about each book −
o Title
o Author
o Subject
o Book ID
 Defining a Structure
To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member. The format of the
struct statement is as follows −
struct [structure tag] {
member definition;
member definition;
...
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 2
member definition;
} [one or more structure variables];
The structure tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At
the end of the structure's definition, before the final semicolon, you can
specify one or more structure variables but it is optional. Here is the way you
would declare the Book structure −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
 Declaring Structure Variables
Different Ways of Declaring Structure Variable:
1) Immediately after Structure Template
struct date
{
int date;
char month[20];
int year;
}today;
// 'today' is name of Structure variable
2) Declare Variables using struct Keyword
struct date
{
int date;
char month[20];
int year;
};
struct date today;
where “date” is name of structure and “today” is name of variable.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 3
3) Declaring Multiple Structure Variables
struct Book
{
int pages;
char name[20];
int year;
}book1, book2, book3;
 Structure Variable Memory Allocation:
Always, contiguous (adjacent) memory locations are used to store structure
members in memory.
Consider below example to understand how memory is allocated for
structures.
#include<stdio.h>
#include<string.h>
struct stud
{
int rNo;
char name[10];
int marks;
};
void main()
{
struct stud s1;
clrscr();
s1.rNo=1;
strcpy(s1.name,"Trupti");
s1.marks = 74;
printf("n Address of s1.rNo = %u", &s1.rNo);
printf("n Address of s1.name = %u", &s1.name);
printf("n Address of s1.marks = %u", &s1.marks);
printf("n Size of s1 = %d bytes",sizeof(s1));
getch();
}
1 T r u p t i 0 74
rNo name marks
65512 65514 65524
14 bytes memory allocated
s1
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 4
 Accessing Structure Members
To access any member of a structure, we use the member access operator (.).
The member access operator is coded as a period between the structure
variable name and the structure member that we wish to access. You would
use the keyword struct to define variables of structure type.
[Reference: http://www.tutorialspoint.com/cprogramming/c_structures.htm ]
Flowchart for Problem Statement:
Algorithm:
1. Start
2. Read the information of students
3. i=0
4. if i<10
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 5
read roll number, name and marks
i=i+1
go to step 4
else
go to step 5
5. Display the information of the students
6. i=0
7. if i<10
display roll number, name and marks
i=i+1
go to step 7
else
go to step 8
8. Stop
Program:
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main(){
struct student s[10];
int i;
printf("Enter information of students:n");
for(i=0;i<10;++i)
{
s[i].roll=i+1;
printf("nFor roll number %dn",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 6
scanf("%f",&s[i].marks);
printf("n");
}
printf("Displaying information of students:nn");
for(i=0;i<10;++i)
{
printf("nInformation for roll number %d:n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
return 0;
}
Input:
Enter information of students:
For roll number 1
Enter name: Sunita
Enter marks: 98
For roll number 2
Enter name: Trupti
Enter marks: 89
.
.
.
Output:
Displaying information of students:
Information for roll number 1:
Name: Sunita
Marks: 98
.
.
.
Practice Problem Statement:
1. Write a C program to add two distance (kilometre, meter) using structure.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 7
2. C Program to Store Book Information(Book title, author, publisher and
Book ID)
Conclusion:
Thus a C program using structure – to create a structure to read and display
students’ records with given members of structure: Roll Number, name and marks
is implemented.
Learning Outcomes:
At the end of this assignment, students are able to
- explain the structure concept.
- differentiate between array and structure.
- define the structure and declare the structure variables.
- read and display records using structure.
- write the program using structure.

More Related Content

What's hot

CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
trupti1976
 
Ocs752 unit 2
Ocs752   unit 2Ocs752   unit 2
Ocs752 unit 2
mgrameshmail
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
mgrameshmail
 
Ocs752 unit 1
Ocs752   unit 1Ocs752   unit 1
Ocs752 unit 1
mgrameshmail
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
SURBHI SAROHA
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
mgrameshmail
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
mgrameshmail
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
SURBHI SAROHA
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
shashikant pabari
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
SURBHI SAROHA
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
Praveen M Jigajinni
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
HINAPARVEENAlXC
 
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
guptkashish
 
important C questions and_answers praveensomesh
important C questions and_answers praveensomeshimportant C questions and_answers praveensomesh
important C questions and_answers praveensomesh
praveensomesh
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
C Token’s
C Token’sC Token’s
C Token’s
Tarun Sharma
 
C programming part4
C programming part4C programming part4
C programming part4
Keroles karam khalil
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
Assignment7
Assignment7Assignment7
Assignment7
Sunita Milind Dol
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
nTier Custom Solutions
 

What's hot (20)

CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
 
Ocs752 unit 2
Ocs752   unit 2Ocs752   unit 2
Ocs752 unit 2
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
 
Ocs752 unit 1
Ocs752   unit 1Ocs752   unit 1
Ocs752 unit 1
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
 
important C questions and_answers praveensomesh
important C questions and_answers praveensomeshimportant C questions and_answers praveensomesh
important C questions and_answers praveensomesh
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
 
C Token’s
C Token’sC Token’s
C Token’s
 
C programming part4
C programming part4C programming part4
C programming part4
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
 
Assignment7
Assignment7Assignment7
Assignment7
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 

Similar to CP Handout#10

Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
psaravanan1985
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
Krishna Nanda
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
Chandrakant Divate
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
babuk110
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
AbhimanyuKumarYadav3
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
1. structure
1. structure1. structure
1. structure
hasan Mohammad
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
Deepak Singh
 
Structures
StructuresStructures
Structures
archikabhatia
 
03 structures
03 structures03 structures
03 structures
Rajan Gautam
 
Structures
StructuresStructures
Structures
selvapon
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
nikshaikh786
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
sumitbardhan
 
Structure
StructureStructure
Structure
ALI RAZA
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
Prabu U
 
Structures
StructuresStructures
Structure In C
Structure In CStructure In C
Structure In C
yndaravind
 
Structures
StructuresStructures
Structures
arshpreetkaur07
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
sangrampatil81
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
sudhakargeruganti
 

Similar to CP Handout#10 (20)

Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Fundamentals of Structure in C Programming
Fundamentals of Structure in C ProgrammingFundamentals of Structure in C Programming
Fundamentals of Structure in C Programming
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
Structure in C
Structure in CStructure in C
Structure in C
 
1. structure
1. structure1. structure
1. structure
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
Structures
StructuresStructures
Structures
 
03 structures
03 structures03 structures
03 structures
 
Structures
StructuresStructures
Structures
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
Structure
StructureStructure
Structure
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
Structures
StructuresStructures
Structures
 
Structure In C
Structure In CStructure In C
Structure In C
 
Structures
StructuresStructures
Structures
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 

More from trupti1976

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3
trupti1976
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10
trupti1976
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
trupti1976
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8
trupti1976
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7
trupti1976
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
trupti1976
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5
trupti1976
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
trupti1976
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2
trupti1976
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1
trupti1976
 

More from trupti1976 (10)

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1
 

Recently uploaded

在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
DuvanRamosGarzon1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 

Recently uploaded (20)

在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSETECHNICAL TRAINING MANUAL   GENERAL FAMILIARIZATION COURSE
TECHNICAL TRAINING MANUAL GENERAL FAMILIARIZATION COURSE
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 

CP Handout#10

  • 1. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 1 Handout#10 Assignment/Program Statement: Write a C program using structure – to create a structure to read and display students’ records with given members of structure: Roll Number, name and marks. Learning Objectives: Students will be able to - explain the structure concept - differentiate between array and structure - define the structure and declare the structure variables - read and display records using structure - write the program using structure Theory:  Arrays allow defining type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type that allows combining data items of different kinds.  Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book − o Title o Author o Subject o Book ID  Defining a Structure To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows − struct [structure tag] { member definition; member definition; ...
  • 2. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 2 member definition; } [one or more structure variables]; The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure − struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book;  Declaring Structure Variables Different Ways of Declaring Structure Variable: 1) Immediately after Structure Template struct date { int date; char month[20]; int year; }today; // 'today' is name of Structure variable 2) Declare Variables using struct Keyword struct date { int date; char month[20]; int year; }; struct date today; where “date” is name of structure and “today” is name of variable.
  • 3. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 3 3) Declaring Multiple Structure Variables struct Book { int pages; char name[20]; int year; }book1, book2, book3;  Structure Variable Memory Allocation: Always, contiguous (adjacent) memory locations are used to store structure members in memory. Consider below example to understand how memory is allocated for structures. #include<stdio.h> #include<string.h> struct stud { int rNo; char name[10]; int marks; }; void main() { struct stud s1; clrscr(); s1.rNo=1; strcpy(s1.name,"Trupti"); s1.marks = 74; printf("n Address of s1.rNo = %u", &s1.rNo); printf("n Address of s1.name = %u", &s1.name); printf("n Address of s1.marks = %u", &s1.marks); printf("n Size of s1 = %d bytes",sizeof(s1)); getch(); } 1 T r u p t i 0 74 rNo name marks 65512 65514 65524 14 bytes memory allocated s1
  • 4. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 4  Accessing Structure Members To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type. [Reference: http://www.tutorialspoint.com/cprogramming/c_structures.htm ] Flowchart for Problem Statement: Algorithm: 1. Start 2. Read the information of students 3. i=0 4. if i<10
  • 5. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 5 read roll number, name and marks i=i+1 go to step 4 else go to step 5 5. Display the information of the students 6. i=0 7. if i<10 display roll number, name and marks i=i+1 go to step 7 else go to step 8 8. Stop Program: #include <stdio.h> struct student{ char name[50]; int roll; float marks; }; int main(){ struct student s[10]; int i; printf("Enter information of students:n"); for(i=0;i<10;++i) { s[i].roll=i+1; printf("nFor roll number %dn",s[i].roll); printf("Enter name: "); scanf("%s",s[i].name); printf("Enter marks: ");
  • 6. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 6 scanf("%f",&s[i].marks); printf("n"); } printf("Displaying information of students:nn"); for(i=0;i<10;++i) { printf("nInformation for roll number %d:n",i+1); printf("Name: "); puts(s[i].name); printf("Marks: %.1f",s[i].marks); } return 0; } Input: Enter information of students: For roll number 1 Enter name: Sunita Enter marks: 98 For roll number 2 Enter name: Trupti Enter marks: 89 . . . Output: Displaying information of students: Information for roll number 1: Name: Sunita Marks: 98 . . . Practice Problem Statement: 1. Write a C program to add two distance (kilometre, meter) using structure.
  • 7. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 7 2. C Program to Store Book Information(Book title, author, publisher and Book ID) Conclusion: Thus a C program using structure – to create a structure to read and display students’ records with given members of structure: Roll Number, name and marks is implemented. Learning Outcomes: At the end of this assignment, students are able to - explain the structure concept. - differentiate between array and structure. - define the structure and declare the structure variables. - read and display records using structure. - write the program using structure.