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.

CP Handout#10

  • 1.
    C-Programming Walchand Institute ofTechnology (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 ofTechnology (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 ofTechnology (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 ofTechnology (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 ofTechnology (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 ofTechnology (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 ofTechnology (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.