Warning Disclaimer Notice:
The below answers and
discussions are directly prepared by copy-paste from www.google.com, Googole AI
Overview, https://gemini.google.com/app
, https://chatgpt.com/,etc,
These are only for
understanding to imrpove the concept only; not for answers. We have not checked
these clearly. Some mistakes may be happened. We are not responsible for any
wrong information and answers. Please check yourself, yourselves and discuss
with your expert teachers for the final approval or accepted the answers.
Structure in C
Example 9.1 A C program where multiple arrays are used to store
information about students.
Answer:
#include<stdio.h>
int main()
{
int roll[20], age[20], studentCount;
printf( "\n How many students? " );
scanf( "%d", &studentCount );
for ( int index = 0; index < studentCount; index++ )
{
printf( " \n Enter roll for student %d ", index );
scanf(" %d", &roll[index] );
printf( " \n Enter age for student %d ", index );
scanf( "%d", &age[index] );
}
printf( "\n Entered details are as follows: \n \n " );
for ( int index = 0; index < studentCount; index++ )
{
printf( " \n Roll for student %d is %d", index,
roll[index] );
printf( " \n Age for student %d is %d", index,
age[index] );
}
return 0;
}
Meaning
& Output:
This C program
collects and displays roll numbers and ages for a user-specified number of
students (up to 20), using parallel arrays roll[20] and age[20]. It prompts for the student count, iterates
to take input for each, and then prints the entered data.
Key Aspects:
·
Input: Accepts an integer for studentCount and
uses a for loop to scan data for each student.
·
Storage: Stores up to 20 integers for roll
numbers and 20 for ages.
·
Output: Displays the stored data in a formatted
list.
·
Safety: The program lacks input validation,
allowing users to enter more than 20 students, which could cause a buffer
overflow.
OUPUT:
This C program prompts
for the number of students, collects their roll numbers and ages, and then
prints the entered data. Assuming 2 students are entered, the output will look
similar to this, depending on user input:
Text
How many students? 2
Enter roll for student 0 101
Enter age for student 0 15
Enter roll for student 1 102
Enter age for student 1 16
Entered details are as follows:
Roll for student 0 is 101
Age for student 0 is 15
Roll for student 1 is 102
Age for student 1 is 16
Example 9.2 A C program demonstrating a simple structure.
Answer:
A complete C program in Example 9.2 where a student structure is created with two fields in it and then
two structure variables are declared. We then assigned some values to them and
displayed those values using printf() statements.
Answer:
#include<stdio.h>
int main()
{
struct Student_Info
{
int roll;
int age;
};
struct Student_Info student1, student2;
student1.roll = 1;
student1.age = 17;
student2.roll = 2;
student2.age = 19;
printf( "Roll and age of the students are: \n ");
printf( "Student 1: Roll: %d Age: %d", student1.roll,
student1.age );
printf( "\n Student 2: Roll: %d Age: %d",
student2.roll, student2.age );
return 0;
}
Meaning
& Output:
The program defines a
structure Student_Info to store a student's roll number and
age. It then creates two instances, assigns values to them, and prints the
details.
Output
text
Roll and age of the students are:
Student 1: Roll: 1 Age: 17
Student 2: Roll: 2 Age: 19
Explanation
1.
Structure
Definition: struct Student_Info contains two integer members: roll and age.
2.
Variable
Declaration: struct Student_Info
student1, student2; creates two
instances of the structure.
3.
Assignment:
1.
student1 is initialized
with roll
= 1 and age = 17.
2.
student2 is initialized
with roll
= 2 and age = 19.
4.
Output
(printf):
0.
printf( "Roll and age of the students are: \n ");: Prints the header line.
1.
printf( "Student 1: Roll: %d Age: %d", student1.roll,
student1.age );: Prints Student 1: Roll: 1
Age: 17.
2.
printf( "\n Student 2: Roll: %d Age: %d",
student2.roll, student2.age );:
Moves to a new line and prints Student 2: Roll: 2 Age: 19.
5.
Return: The function returns 0, indicating
successful execution.
Example 9.3
Another C program demonstrating a simple structure.
Answer:
We may also scan values to the structure variable fields from the
keyboard. Example 9.3 shows this.
#include<stdio.h>
int main()
{
struct Student_Info
{
int roll;
int age;
};
struct Student_Info
student1, student2;
printf( "\n
Enter roll of student 1: " );
scanf("%d",
&student1.roll );
printf( "\n
Enter age of student 1: " );
scanf("%d",
&student1.age );
printf( "\n
Enter roll of student 2: " );
scanf("%d",
&student2.roll );
printf( "\n
Enter age of student 2: " );
scanf("%d",
&student2.age );
printf( "Roll
and age of the students are: \n ");
printf( "Student
1: Roll: %d Age: %d", student1.roll, student1.age );
printf( "\n
Student 2: Roll: %d Age: %d", student2.roll, student2.age );
return 0;
}
Meaning & Output
(AI):
This is a classic example of using Structures
in C. Your code correctly defines a template (Student_Info) and creates two "instances" (student1 and
student2) to store data.
When you use a structure, you are
essentially telling the computer to group different variables together in one
block of memory.
How
this works in memory
Instead of having four separate
variables floating around, the computer allocates a single block for student1 and another for student2. Inside each block, the roll and age
are stored side-by-side.
A
Few Tips for your Code:
1.
Placement
of Struct Definition: Usually, it is better to define
your struct outside of main(). If you define it inside main, only the main function can use it. If you move it to the top of the file,
you can create other functions (like displayStudent) that also recognize the Student_Info type.
2.
Formatting: Be careful with the \n (newline) and text. In your code, there are some line
breaks inside the printf
quotes that might cause the compiler to throw an error depending on your IDE.
It's best to keep the string on one line.
Refined
Version
Here is your code with the struct
moved to a global scope and the formatting cleaned up:
C
#include
<stdio.h>
//
Define the structure outside so it's accessible everywhere
struct
Student_Info {
int
roll;
int age;
};
int
main() {
struct Student_Info student1, student2;
// Student 1 Input
printf("Enter roll of student 1:
");
scanf("%d", &student1.roll);
printf("Enter age of student 1: ");
scanf("%d", &student1.age);
// Student 2 Input
printf("\nEnter roll of student 2:
");
scanf("%d", &student2.roll);
printf("Enter age of student 2: ");
scanf("%d", &student2.age);
// Output
printf("\n--- Student Details
---\n");
printf("Student 1: Roll: %d, Age:
%d\n", student1.roll, student1.age);
printf("Student 2: Roll: %d, Age:
%d\n", student2.roll, student2.age);
return 0;
}
Would you
like to see how to use an "Array of Structures" so you can handle 10
or 100 students without creating student1, student2, student3 manually?
OUTPUT:
When you run this code, the program
will prompt you to enter the data for each student one by one. Once you provide
the numbers, it will print them back to you in a formatted list.
Assuming you enter 101 and 18
for the first student, and 102 and 19 for the second, here is
exactly what the console will look like:
Expected
Output
Plaintext
Enter
roll of student 1: 101
Enter
age of student 1: 18
Enter
roll of student 2: 102
Enter
age of student 2: 19
Roll
and age of the students are:
Student 1: Roll: 101 Age: 18
Student 2: Roll: 102 Age: 19
How
the Data is Stored
Behind the scenes, the structure
acts like a container. Instead of having separate variables for everything, C
groups the roll
and age together for each student in
memory.
A
Note on the "Newlines" in your Code
In the code you provided, there are
literal line breaks inside your printf strings (e.g., printf("\n\nEnter...");).
- In a real compiler: This might cause a "missing terminating
character" error.
- The Fix: Always keep your text inside the quotes on a single
line. Use \n whenever you want a new line to appear in the output.
Would you
like me to show you how to use a for loop so you can input 5 students without writing the same
code 5 times?
Example 9.4 C
program demonstrating the use of a pointer with a structure.
Example 9.4 shows a simple C program where a pointer is used to scan and
display the member variables of a structure Student_Info. We see that a pointer
variable of type struct Student_Info is declared in line number 10. Then this
pointer is made to point to a structure variable student (declared in line
number 9) in line number 11. Once the pointer ptr points to the structure
variable, we use the notations discussed above for accessing the members of
that structure.
Answer:
#include<stdio.h>
int main()
{
struct Student_Info
{
int roll;
int age;
};
struct Student_Info
student;
struct Student_Info
*ptr;
ptr = &student;
printf( " \n
Enter roll of the student: " );
scanf("
%d", &ptr->roll );
printf( " \n
Enter age of the student: " );
scanf(
"%d", &ptr->age );
printf( "\n
Entered details are as follows: \n \n " );
printf( " \n
Roll of the student is %d", ptr->roll );
printf( " \n
Age of the student is %d", ptr->age );
return 0;
}
Meaning
& Output:
This
C program defines a struct Student_Info to store a
student's roll (integer) and age (integer). Using a pointer (ptr) to the structure, it prompts for input,
reads the data, and displays the entered details, showcasing how to access
structure members with pointers.
Code
Breakdown:
·
Structure Definition: struct Student_Info defines a data structure with roll and age fields.
·
Pointer Usage: ptr = &student; assigns the address of the student structure to the pointer ptr.
·
Input (scanf): The
program uses ptr->roll and ptr->age to access and store the input values in the student structure using the pointer.
·
Output (printf): Finally,
it displays the entered roll and age using the same pointer-member access
notation.
OUTPUT:
The
provided C program defines a student structure, uses a pointer to take user
input for roll number and age, and then displays the entered details. Assuming
input 101 for roll and 20 for age, the expected output is:
text
Enter roll of the student: 101
Enter age of the student: 20
Entered details are as follows:
Roll of the student is 101
Age of the student is 20
Key
Points:
·
ptr = &student;: The pointer ptr is initialized to point to the student structure.
·
ptr->roll: The arrow operator (->) is used to access structure members via a pointer.
·
Input: The program prompts for user input twice, storing values
directly into the student structure
fields via ptr.
·
Output: The final lines display the stored values.
Example 9.5 C
program demonstrating the use of a single pointer with multiple structure
variables.
Example 9.5 shows a C program where a single pointer *ptr is used to point to
two structure variables student1 and student2. The pointer first points to
student1 and it scans values from the keyboard. Then, it is made to point to
student2. So it scans values for the next variable.
To display the student record, we do the same thing. First the
pointer points to student1 (line number 22) and displays its member variables.
Then, it points to student2 (line number 25) and displays its member variables.
Answer:
#include<stdio.h>
int main()
{
struct Student_Info
{
int roll;
int age;
};
struct Student_Info
student1, student2;
struct Student_Info
*ptr;
ptr =
&student1; // pointer points to student1
printf( " \n
Enter roll of the student 1: " );
scanf("
%d", &ptr->roll );
printf( " \n
Enter age of the student 1: " );
scanf(
"%d", &ptr->age );
ptr =
&student2; // pointer points to student2
printf( " \n
Enter roll of the student 2: " );
scanf("
%d", &ptr->roll );
printf( " \n
Enter age of the student 2: " );
scanf(
"%d", &ptr->age );
printf( "\n
Entered details are as follows: \n \n " );
ptr =
&student1; // pointer ptr points to student1 again
printf( " \n
Roll of the student 1 is %d", ptr->roll );
printf( " \n
Age of the student 1 is %d", ptr->age );
ptr =
&student2; pointer ptr now points to student2
printf( " \n
Roll of the student 2 is %d", ptr->roll );
printf( " \n
Age of the student 2 is %d", ptr->age );
return 0;
}
Meaning
& Output:
The provided C code
demonstrates how to use pointers to access and modify structure members.
However, it contains one syntax error related to comments and could be improved
for better readability.
Syntax Error Correction
In the line:
ptr =
&student2; pointer ptr now points to student2
The text pointer ptr now points
to student2 is not a valid
comment. In C, single-line comments must start with //. This will cause a compilation error.
Corrected line:
ptr =
&student2; // pointer ptr now points to student2
Corrected Code
c
#include<stdio.h>
int
main()
{
struct Student_Info
{
int
roll;
int
age;
};
struct Student_Info student1,
student2;
struct Student_Info *ptr;
// Point to student1 and take input
ptr = &student1;
printf( " \n Enter roll of the student
1: " );
scanf("%d",
&ptr->roll );
printf( " \n Enter age of the student
1: " );
scanf("%d",
&ptr->age );
// Point to student2 and take input
ptr = &student2;
printf( " \n Enter roll of the student
2: " );
scanf("%d",
&ptr->roll );
printf( " \n Enter age of the student
2: " );
scanf("%d",
&ptr->age );
printf( "\n Entered details are as
follows: \n \n " );
// Display student 1 details
ptr = &student1;
printf( " \n Roll of the student 1 is
%d", ptr->roll );
printf( " \n Age of the student 1 is
%d", ptr->age );
// Display student 2 details
ptr = &student2;
printf( " \n Roll of the student 2 is
%d", ptr->roll );
printf( " \n Age of the student 2 is
%d", ptr->age );
return 0;
}
Key Concepts Demonstrated
1.
Structure
Pointers (struct
Student_Info *ptr): Declares a pointer capable of holding
the address of a Student_Info struct.
2.
Pointer
Assignment (ptr = &student1): Assigns the
memory address of a struct variable to the pointer.
3.
Member
Access Operator (->): Uses ptr->roll to access structure members through the pointer. This is
equivalent to (*ptr).roll.
4.
scanf with
pointers: scanf requires the address of the variable,
which is naturally provided by &ptr->roll (or ptr->roll if ptr already holds
the struct address).
Output Example
text
Enter roll of the student 1: 101
Enter age of the student 1: 20
Enter roll of the student 2: 102
Enter age of the student 2: 21
Entered details are as follows:
Roll of the student 1 is 101
Age of the student 1 is 20
Roll of the student 2 is 102
Age of the student 2 is 21
Example 9.6 A
C program demonstrating an array of structure variables.
Answer:
#include<stdio.h>
int main()
{
struct Student_Info
{
int roll;
int age;
};
struct Student_Info
student[10];
int studentCount;
printf( "\n
How many students? " );
scanf(
"%d", &studentCount );
for ( int index =
0; index < studentCount; index++ )
{
printf( " \n Enter roll for student %d ", index );
scanf(" %d", &student[index].roll );
printf( " \n Enter age for student %d ", index );
scanf( "%d", &student[index].age );
}
printf( "\n
Entered details are as follows: \n \n " );
for ( int index =
0; index < studentCount; index++ )
{
printf( " \n Roll for student %d is %d", index,
student[index].roll );
printf( " \n Age for student %d is %d", index,
student[index].age );
}
return 0;
}
Meaning
& Output:
This C program defines
a Student_Info structure, creates an array of 10
students, and uses a loop to prompt for input (studentCount, roll, age) before displaying the details. It efficiently
collects data for up to 10 students and prints them back, demonstrating basic
struct usage and array handling.
Key Code Features:
·
Struct
Definition: struct Student_Info {
int roll; int age; }; organizes
student data.
·
Array
Allocation: struct Student_Info
student[10]; reserves memory
for 10 students.
·
Input
Handling: scanf is used within a for loop to capture user input for each
student's roll and age.
·
Output
Display: A second for loop prints the entered data for each
student.
Potential
Improvements:
·
Boundary
Checking: The code
assumes studentCount will not exceed 10. Adding if (studentCount >
10) studentCount = 10; would prevent
potential overflow.
·
Input
Validation: Validating
that studentCount is a positive number.
OUTPUT:
This C program prompts
for the number of students, collects their roll numbers and ages, and then
prints the entered data. Assuming a user inputs 2 students with specific
details, the output will look similar to the following:
text
How many students? 2
Enter roll for student 0 101
Enter age for student 0 15
Enter roll for student 1 102
Enter age for student 1 16
Entered details are as follows:
Roll for student 0 is 101
Age for student 0 is 15
Roll for student 1 is 102
Age for student 1 is 16
Key Aspects of the
Program:
·
Struct
Definition: Defines a struct Student_Info containing roll and age.
·
Array
Allocation: Allocates space
for 10 students (struct Student_Info student).
·
Input
Handling: Uses scanf to store data into the student array.
·
Output
Formatting: Displays the
data in the same order it was entered.
Example 9.7
Another C program demonstrating an array of structure variables.
Example 9.7. In this program, we have added a new field (name) in the definition of the structure Student_Info. So we are to take
input in this new field. We have taken input using a gets() function.
Answer:
#include<stdio.h>
int main()
{
struct Student_Info
{
char name[30];
int roll;
int age;
};
struct Student_Info
student[10];
int studentCount;
printf( "\n
How many students? " );
scanf(
"%d", &studentCount );
for ( int index =
0; index < studentCount; index++ )
{
printf( " \n Enter name for student %d ", index );
getchar();
gets( student[index].name );
printf( " \n Enter roll for student %d ", index );
scanf(" %d", &student[index].roll );
printf( " \n Enter age for student %d ", index );
scanf( "%d", &student[index].age );
}
printf( "\n
Entered details are as follows: \n \n " );
for ( int index =
0; index < studentCount; index++ )
{
printf("\n\n Student %d: ", index+1 );
printf("\n Name: ");
puts( student[index].name );
printf( "
Roll: %d", student[index].roll );
printf( " \n Age: %d", student[index].age );
}
return 0;
}
Meaning
& Output:
This C program defines
a Student_Info structure, creates an array of 10
students, prompts for input (count, name, roll, age), and displays them. It
uses getchar() to consume the newline from scanf() and gets() for string input.
Warning: Using gets() is highly discouraged in modern C as it is unsafe.
Key Aspects:
·
Structure: struct Student_Info { char name[30]; int roll;
int age; };
·
Array: struct Student_Info student[10]; (Allows storing up to 10 students).
·
Input
Handling: scanf("%d",
&studentCount); reads how many
students. getchar(); is crucial to consume the newline
character left by scanf so gets() works correctly.
·
Output: The code uses puts() to print the student names.
·
Limitation: If studentCount exceeds 10, the program will experience a buffer overflow.
Potential
Improvements:
·
Use fgets(student[index].name,
30, stdin); instead of gets().
·
Add a check for studentCount <= 10
Based on the code
provided, the program prompts for the number of students, then takes input
(name, roll, age) for each student using gets() and scanf(). It subsequently
displays the entered data formatted with "Student X",
"Name", "Roll", and "Age".
Example Output
(Assuming 2 students):
text
How many students? 2
Enter name for student 0 : John Doe
Enter roll for student 0 : 101
Enter age for student 0 : 20
Enter name for student 1 : Jane Smith
Enter roll for student 1 : 102
Enter age for student 1 : 19
Entered details are as follows:
Student 1:
Name: John Doe
Roll: 101
Age: 20
Student 2:
Name: Jane Smith
Roll: 102
Age: 19
Note: gets() is considered unsafe in modern C
programming and can cause buffer overflows if input exceeds 30 characters.
Example 9.8 A
C program for handling information about books.
Answer:
#include<stdio.h>
int main()
{
struct Book_Info
{
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct Book_Info
book[10];
int totalBookCount;
printf( "\n
How many books in total? " );
scanf(
"%d", &totalBookCount );
for ( int index =
0; index < totalBookCount; index++ )
{
printf( " \n Enter title of the book %d ", index );
getchar();
gets(book[index].title);
printf( " \n Enter Book ID %d ", index );
scanf(" %d", &book[index].bookID );
printf( " \n Enter total no of copies %d ", index );
scanf(" %d", &book[index].noOfCopies );
printf( " \n Enter author of the book %d ", index );
getchar();
gets(book[index].author);
}
printf( "\n
Entered details are as follows: \n \n " );
for ( int index =
0; index < totalBookCount; index++ )
{
printf("\n
Book %d \n", index);
printf(" Title: ");
puts( book[index].title );
printf( " ID: %d \n Available copies: %d ",
book[index].bookID,
book[index].noOfCopies );
printf(" \n Author: ");
puts( book[index].author );
}
return 0;
}
Meaning & Output:
Meaning & Output (from Gemini AI):
This code demonstrates an Array
of Structures. It is much more efficient than the previous example because
it uses a single array book[10] to handle multiple books using a for loop.
If you run this code and specify
that you have 2 books, here is how the output will look:
Expected
Output
Plaintext
How
many books in total? 2
Enter title of the book 0: Hamlet
Enter Book ID 0: 101
Enter total no of copies 0: 5
Enter author of the book 0: William
Shakespeare
Enter title of the book 1: 1984
Enter Book ID 1: 102
Enter total no of copies 1: 3
Enter author of the book 1: George Orwell
Entered
details are as follows:
Book
0
Title: Hamlet
ID: 101
Available copies: 5
Author: William Shakespeare
Book
1
Title: 1984
ID: 102
Available copies: 3
Author: George Orwell
How
it works: Array of Structures
Instead of creating book1, book2,
etc., you created an array. Each "slot" in the array is a complete Book_Info structure.
, book[1] each containing title, ID,
copies, and author]
Important
Technical Notes:
- Buffer Clearing:
Your use of getchar() before gets() is a smart move. It catches the "Enter" key
(newline character) left over from the scanf calls. Without it, the program would skip the title or
author inputs.
- Array Limits:
You defined book[10]. If a user enters a "totalBookCount" greater
than 10, the program will crash because it will try to access memory that
doesn't belong to the array (this is called Buffer Overflow).
- Modern Safety:
As a reminder, gets() is considered unsafe in modern C. Most professional
programmers use fgets(book[index].title, 30,
stdin); because it ensures the user
doesn't type more than 30 characters and break the program.
Would you like me to show you how to
search for a specific book by its ID within this array?
Example 9.9 A C program for handling information about books
with an issue operation.
Example 9.9. We can see that the structure is declared as a global variable
along with the total number of records. We have used three userdefined
functions for the following purposes.
Function name: Purpose
input(): To take input to the variables from the keyboard,
output(): To display the records,
issue(): To perform an issue operation for a book,
As we have declared the structure as global, all the functions can
access it. We have called the issue() function only once. This means that issue
operation is performed only once for a book.
We have called the output() function from the issue() function
(line number 10 of the function definition) to display the total book record
after decreasing the available quantity of the book.
After we finished our work, we wrote a return statement to come
out from the function (line number 11 of the function definition). We can do so
because we have already found the book record and we performed the issue
operation. If we do not return, the loop will unnecessarily iterate till the
last record.
Answer:
#include<stdio.h>
struct Book_Info
{
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct Book_Info book[10];
int totalRecord;
void input()
{
for ( int index =
0; index < totalRecord; index++ )
{
printf( " \n Enter title of the book %d ", index );
gets(book[index].title);
printf( " \n Enter Book ID %d ", index );
scanf(" %d", &book[index].bookID );
printf( " \n Enter total no of copies %d ", index );
scanf(" %d", &book[index].noOfCopies );
printf( " \n Enter author of the book %d ", index );
getchar();
gets(book[index].author);
}
}
void output()
{
printf( "\n
Entered details are as follows: \n \n " );
for ( int index =
0; index < totalRecord; index++ )
{
printf("\n Book %d \n\n", index);
printf(" Title: ");
puts(book[index].title);
printf( " ID: %d \n Remaining copies: %d \n",
book[index].bookID,
book[index].noOfCopies );
printf(" Authors: ");
puts(book[index].author);
}
}
void issue()
{
int id;
printf("\n
Enter the ID of the book to issue: ");
scanf("%d",
&id);
for ( int index =
0; index < totalRecord; index++ )
{
if( book[index].bookID == id )
{
book[index].noOfCopies = book[index].noOfCopies - 1;
printf("\n One book issued with ID: %d", id);
output();
return;
}
}
}
int main()
{
printf( "\n
How many books in total? " );
scanf(
"%d", &totalRecord );
getchar();
input();
output();
issue();
return 0;
}
Meaning & Output:
This C program defines
a struct
Book_Info to manage
library records (title, ID, copies, author) for up to 10 books using an array.
It includes functions for user input (input), displaying inventory (output), and issuing a book (issue) by reducing the copy count, featuring a simple main menu flow.
Key Features and
Structure:
·
Struct: struct Book_Info stores Title, ID, Copies, and Author.
·
Data
Structure: struct Book_Info
book[10]; stores up to 10
books.
·
Input
(input): Uses gets() for string input
(unsafe, replaced in modern code) and scanf() for integers. getchar() clears the newline buffer.
·
Output
(output): Displays current inventory using puts().
·
Issue
(issue): Decrements noOfCopies for a matching bookID.
·
Main
Function: Manages total
records and calls input, output, and issue functions sequentially.
Example Output Flow:
1.
Asks for the total
number of books.
2.
Prompts for details
for each book.
3.
Displays the entered
details.
4.
Asks for a book ID to
issue and reduces its count.
Note: The use of gets() is insecure and has been removed from
the C11 standard. fgets() is
recommended for safer string input.
Based on the code, the
program prompts for the total number of books, accepts details (title, ID,
copies, author) for each, prints them, and then reduces the copy count based on
a user-inputted ID.
Sample Output:
text
How many books in total? 2
Enter title of the book 0 C Programming
Enter Book ID 0 101
Enter total no of copies 0 5
Enter author of the book 0 Dennis Ritchie
Enter title of the book 1 Data Structures
Enter Book ID 1 102
Enter total no of copies 1 3
Enter author of the book 1 E. Balagurusamy
Entered details are as follows:
Book 0
Title: C Programming
ID: 101
Remaining copies: 5
Authors: Dennis Ritchie
Book 1
Title: Data Structures
ID: 102
Remaining copies: 3
Authors: E. Balagurusamy
Enter the ID of the book to issue: 101
One book issued with ID: 101
Entered details are as follows:
Book 0
Title: C Programming
ID: 101
Remaining copies: 4
Authors: Dennis Ritchie
Book 1
Title: Data Structures
ID: 102
Remaining copies: 3
Authors: E. Balagurusamy
Key Behaviors:
·
gets() Usage: gets() reads the entire line, including spaces, for titles and
authors.
·
Buffer
Clearing: getchar() is used after scanf to consume the newline character,
ensuring gets() works properly.
·
Issue Functionality: It searches by bookID, decrements noOfCopies by 1, and reprints all records
using output().
· Limitations: gets() is insecure and should be replaced by fgets() in modern C, though it works in this example.
Example 9.10
A C program for handling information about books with an issue operation.
Example 9.10. We see that all the portions of the program are the same except
the returnBook() function. Accordingly, instead of calling issue() function, we
called returnBook() function (line number 6 in main() function).
Answer:
#include<stdio.h>
struct Book_Info
{
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct Book_Info book[10];
int totalRecord;
void input()
{
for ( int index = 0; index < totalRecord; index++ )
{
printf( " \n Enter title of the book %d ", index );
gets(book[index].title);
printf( " \n Enter Book ID %d ", index );
scanf(" %d", &book[index].bookID );
printf( " \n Enter total no of copies %d ", index );
scanf(" %d", &book[index].noOfCopies );
printf( " \n Enter author of the book %d ", index );
getchar();
gets(book[index].author);
}
}
void output()
{
printf( "\n Entered details are as follows: \n \n " );
for ( int index = 0; index < totalRecord; index++ )
{
printf("\n Book %d \n\n", index);
printf(" Title: ");
puts(book[index].title);
printf( " ID: %d \n Remaining copies: %d \n",
book[index].bookID,
book[index].noOfCopies );
printf(" Authors: ");
puts(book[index].author);
}
}
void returnBook()
{
int id;
printf("\n Enter the ID of the book to return: ");
scanf("%d", &id);
for ( int index = 0; index < totalRecord; index++ )
{
if( book[index].bookID = = id )
{
book[index].noOfCopies =
book[index].noOfCopies + 1;
printf("\n One book is returned with ID: %d", id);
output();
return;
}
}
}
int main()
{
printf( "\n How many books in total? " );
scanf( "%d", &totalRecord );
getchar();
input();
output();
returnBook();
return 0;
}
Meaning
& Output:
The provided C code
defines a simple library management system to input, output, and return book
records using structures. It features a struct Book_Info, input/output functions, and
a returnBook() function that increments the
copy count. However, it contains unsafe code (gets()) and potential issues with input buffering.
Key Issues and
Improvements:
·
Safety
Hazard: gets() is dangerous and deprecated. Use fgets() instead to prevent buffer overflows.
·
Input
Handling: scanf leaves newline characters in the buffer,
which can cause fgets or gets to skip inputs.
·
returnBook() Fix: The
current if condition book[index].bookID = =
id is incorrect
(uses an assignment operator if written as =). It must be == to compare.
Corrected Code:
c
#include<stdio.h>
#include<string.h> // Required for strcspn
struct
Book_Info {
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct
Book_Info book[10];
int
totalRecord;
// Function to safely
remove newline from fgets
void
stripNewline(char *str) {
str[strcspn(str, "\r\n")] =
0;
}
void
input() {
for ( int
index = 0; index < totalRecord; index++ )
{
printf( "\n
Enter title of the book %d: ", index );
fgets(book[index].title, sizeof(book[index].title), stdin);
stripNewline(book[index].title);
printf( "
Enter Book ID %d: ", index );
scanf("%d",
&book[index].bookID );
printf( "
Enter total no of copies %d: ", index );
scanf("%d",
&book[index].noOfCopies );
getchar(); //
Clear newline from scanf
printf( "
Enter author of the book %d: ", index );
fgets(book[index].author, sizeof(book[index].author), stdin);
stripNewline(book[index].author);
}
}
void
output() {
printf( "\n --- Entered details ---
\n" );
for ( int
index = 0; index < totalRecord; index++ )
{
printf("\n
Book %d", index);
printf("\n
Title: %s", book[index].title);
printf("\n
ID: %d \n Remaining copies: %d", book[index].bookID,
book[index].noOfCopies );
printf("\n
Authors: %s\n", book[index].author);
}
}
void
returnBook() {
int id;
printf("\n Enter the ID of the book to
return: ");
scanf("%d", &id);
for ( int
index = 0; index < totalRecord; index++ )
{
//
Corrected comparison operator: ==
if(
book[index].bookID == id ) {
book[index].noOfCopies++;
printf("
One book is returned with ID: %d\n", id);
output();
return;
}
}
printf("Book ID not found.\n");
}
int
main() {
printf( "How many books in total?
" );
scanf( "%d",
&totalRecord );
getchar(); // Clear buffer
input();
output();
returnBook();
return 0;
}
OUTPUT:
Based on the code
provided, the program takes input for the number of books, their details
(title, ID, copies, author), prints them, asks to return a book by ID,
increases the copy count, and prints the updated inventory. Assuming a total of
2 books are entered, the output would be:
text
How many books in total? 2
Enter title of the book 0: The C
Programming Language
Enter Book ID 0: 101
Enter total no of copies 0: 5
Enter author of the book 0: Ritchie
Enter title of the book 1: Data
Structures
Enter Book ID 1: 102
Enter total no of copies 1: 3
Enter author of the book 1: Sahni
Entered details are as follows:
Book 0
Title: The C Programming Language
ID: 101
Remaining copies: 5
Authors: Ritchie
Book 1
Title: Data Structures
ID: 102
Remaining copies: 3
Authors: Sahni
Enter the ID of the book to return:
101
One book is returned with ID: 101
Entered details are as follows:
Book 0
Title: The C Programming Language
ID: 101
Remaining copies: 6
Authors: Ritchie
Book 1
Title: Data Structures
ID: 102
Remaining copies: 3
Authors: Sahni
Note regarding
potential bugs: The code
contains a space between = and = in if( book[index].bookID = = id ). To run successfully, this must be == (equality operator) instead of = =.
Figure 9.2: A running instance of the required application.
1: Display all book details
2: Display particular book details
3: Issue a book
4: Return a book
5: Quit the application
Please enter your choice: __
Now, when we want some statements to run continuously, we can use
a while (1) loop as
follows.
while (1)
{
. . .
}
We need to place the options (as shown in Figure 9.2) inside the while
loop so that they appear everytime. Once the user enters an option, we need to
perform the required task associated with that option. For instance, the user
enters 1, then we need to display all the records.
We can use a switch-case construct for this. In each case of the
switch-case, we can call the appropriate function to do the required task. The
following code segment shows this clearly.
We call the function displayAll() in case 1 and this will be
executed when the user enters option 1. Similarly, when the user enters 4, it
belongs to case 4 and we are calling the function returnBook().
while (1)
{
printf(" \n 1: Display all book details" );
printf(" \n 2: Display particular book details" );
printf(" \n 3: Issue a book" );
printf(" \n 4: Return a book" );
printf(" \n 5: Quit the application" );
printf(" \n Please enter your choice: " );
scanf( "%d", &option );
switch
(option)
{
case
1:
displayAll();
break;
case
2:
displayOne();
break;
case
3:
issueBook();
break;
case
4:
returnBook();
break;
case
5:
return
0;
default:
printf("
\n Invalid choice! ");
}
Example 9.11 A C program for a mini library application.
Example 9.11. All other portions of the program are similar to the previous
ones.
Answer:
#include<stdio.h>
struct Book_Info
{
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct Book_Info book[10];
int totalRecord;
void input()
{
for ( int index = 0; index < totalRecord; index++ )
{
printf( " \n Enter title of the book %d ", index );
gets(book[index].title);
printf( " \n Enter Book ID %d ", index );
scanf(" %d", &book[index].bookID );
printf( " \n Enter total no of copies %d ", index );
scanf(" %d", &book[index].noOfCopies );
printf( " \n Enter author of the book %d ", index );
getchar();
gets(book[index].author);
}
}
void displayAll()
{
printf( "\n The library has the following books: \n \n
" );
for ( int index = 0; index < totalRecord; index++ )
{
printf("\n Book %d \n\n", index);
printf(" Title: ");
puts(book[index].title);
printf( " ID: %d \n Remaining copies: %d \n",
book[index].bookID,
book[index].noOfCopies );
printf(" Authors: ");
puts(book[index].author);
}
}
void displayOne()
{
int id;
printf("\n Enter the ID of the book to issue: ");
scanf("%d", &id);
for ( int index = 0; index < totalRecord; index++ )
{
if( book[index].bookID == id )
{
printf(" Title: ");
puts(book[index].title);
printf( " ID: %d \n Remaining copies: %d \n",
book[index].bookID,
book[index].noOfCopies );
printf(" Authors: ");
puts(book[index].author);
}
}
}
void issueBook()
{
int id;
printf("\n Enter the ID of the book to issue: ");
scanf("%d", &id);
for ( int index = 0; index < totalRecord; index++ )
{
if( book[index].bookID = = id )
{
book[index].noOfCopies = book[index].noOfCopies - 1;
printf("\n One book is returned with ID: %d", id);
return;
}
}
}
void returnBook()
{
int id;
printf("\n Enter the ID of the book to return: ");
scanf("%d", &id);
for ( int index = 0; index < totalRecord; index++ )
{
if( book[index].bookID = = id )
{
book[index].noOfCopies = book[index].noOfCopies + 1;
printf("\n One book is returned with ID: %d", id);
return;
}
}
}
int main()
{
printf( "\n How many books in total? " );
scanf( "%d", &totalRecord );
getchar();
input();
int option;
while (1)
{
printf(" \n 1: Display all book details" );
printf(" \n 2: Display particular book details" );
printf(" \n 3: Issue a book" );
printf(" \n 4: Return a book" );
printf(" \n 5: Quit the application" );
printf(" \n Please enter your choice: " );
scanf( "%d", &option );
switch (option)
{
case 1:
displayAll();
break;
case 2:
displayOne();
break;
case 3:
issueBook();
break;
case 4:
returnBook();
break;
case 5:
return 0;
default:
printf(" \n Invalid choice! ");
}
}
return 0;
}
This code ties everything together
into a functional Library Management System. You have implemented a
menu-driven program using a while(1) loop and a switch statement, which is the standard way to build interactive
console applications in C.
However, there are a few syntax
errors and logical bugs that will prevent this specific version from compiling
or running correctly.
1.
Critical Syntax Errors
- The Assignment Bug:
In issueBook() and returnBook(), you wrote if(
book[index].bookID = = id ).
In C, the equality operator must not have a space. It should be ==.
- The String Formatting: In displayAll(), there is a line break inside your printf
quotes. This will cause a compilation error.
- Output Message:
In issueBook(), your success message says "One book is returned"
instead of "issued."
2.
Logic & Safety Fixes
- The Stock Issue:
In issueBook(), you should check if noOfCopies
> 0 before subtracting. Otherwise,
your library could end up with -1 books.
- The gets() Issue:
As we discussed, gets() is risky. Also, mixing scanf and gets can cause the program to skip inputs because of the
"newline" character left in the buffer.
Corrected
& Optimized Version
Here is the fixed version of your
code. I've added a few safety checks (like checking if a book exists before
issuing) to make it more robust.
C
#include<stdio.h>
#include<string.h>
struct
Book_Info {
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct
Book_Info book[10];
int
totalRecord;
void
input() {
for (int index = 0; index < totalRecord;
index++) {
printf("\n--- Book %d ---",
index + 1);
printf("\nEnter title: ");
// Using %[^\n] reads the full title
including spaces
scanf(" %29[^\n]",
book[index].title);
printf("Enter Book ID: ");
scanf("%d",
&book[index].bookID);
printf("Enter total copies:
");
scanf("%d", &book[index].noOfCopies);
printf("Enter author: ");
scanf(" %29[^\n]",
book[index].author);
}
}
void
displayAll() {
printf("\n--- Library Records
---");
for (int index = 0; index < totalRecord;
index++) {
printf("\nID: %d | Title: %s |
Copies: %d | Author: %s",
book[index].bookID,
book[index].title,
book[index].noOfCopies,
book[index].author);
}
printf("\n");
}
void
issueBook() {
int id, found = 0;
printf("\nEnter Book ID to issue:
");
scanf("%d", &id);
for (int index = 0; index < totalRecord;
index++) {
if (book[index].bookID == id) {
found = 1;
if (book[index].noOfCopies > 0)
{
book[index].noOfCopies--;
printf("Book issued! Remaining:
%d\n", book[index].noOfCopies);
} else {
printf("Error: Out of
stock!\n");
}
break;
}
}
if (!found) printf("Error: Book ID not
found.\n");
}
void
returnBook() {
int id, found = 0;
printf("\nEnter Book ID to return:
");
scanf("%d", &id);
for (int index = 0; index < totalRecord;
index++) {
if (book[index].bookID == id) {
book[index].noOfCopies++;
printf("Book returned! Total
copies: %d\n", book[index].noOfCopies);
found = 1;
break;
}
}
if (!found) printf("Error: Book ID not
found.\n");
}
int
main() {
printf("How many books to register
(max 10)? ");
scanf("%d", &totalRecord);
input();
int option;
while (1) {
printf("\n1: All | 2: Issue | 3:
Return | 4: Exit\nChoice: ");
scanf("%d", &option);
if (option == 1) displayAll();
else if (option == 2) issueBook();
else if (option == 3) returnBook();
else if (option == 4) break;
else printf("Invalid
choice!\n");
}
return 0;
}
Visualizing
the Program Flow
When you run this, the main() function acts as the "Air Traffic Controller,"
sending the program to different functions based on your input.
OUTPUT:
To show you the output, I'll
simulate a session where we register 2 books and then issue one
of them.
Because your code has a few syntax
issues (like the space in = =), the output below assumes those are fixed so the program
can actually run.
Simulation
Trace
Initial Setup
Plaintext
How many books in total? 2
Enter title of the book 0: The Great Gatsby
Enter Book ID 0: 101
Enter total no of copies 0: 5
Enter author of the book 0: F. Scott
Fitzgerald
Enter title of the book 1: 1984
Enter Book ID 1: 102
Enter total no of copies 1: 3
Enter author of the book 1: George Orwell
Using the Menu (Issuing a Book)
Plaintext
1: Display all book details
2: Display particular book details
3: Issue a book
4: Return a book
5: Quit the application
Please enter your choice: 3
Enter the ID of the book to issue: 102
One book is issued with ID: 102
Verifying the Change (Displaying
All)
Plaintext
1: Display all book details
2: Display particular book details
3: Issue a book
4: Return a book
5: Quit the application
Please enter your choice: 1
The library has the following books:
Book 0
Title: The Great Gatsby
ID: 101
Remaining copies: 5
Authors: F. Scott Fitzgerald
Book 1
Title: 1984
ID: 102
Remaining copies: 2
Authors: George Orwell
How
the Data is Managed
When the program runs, it creates an
array of structures in the Stack memory. Each element in the array book[index] holds all the fields (title, ID, etc.) for that specific
entry.
and book[1]]
Important
Correction for your code:
In your issueBook and returnBook functions, look at this line: if(
book[index].bookID = = id )
- What happens:
The compiler will give you an error because of the space.
- The Fix:
Change it to ==.
- Logic Note:
In issueBook, your code currently prints "One book is returned."
You'll likely want to change that text to "issued" so the
user doesn't get confused!
Would you like me to explain how the
getchar() and gets() combination works to handle those book titles with spaces?
Quick
activity: Extend the previous program of to handle some unusual
conditions such as a user wants to issue a book but (i) it is not there in the
library or (ii) the available quantity of the book is 0.
Answer:
Actually, your existing code
already handles the second condition (available quantity is 0) quite well!
However, we can refine the logic to make the feedback to the user more specific
and "unusual-condition-proof."
Here is how we
can extend the issueBook() function to handle those two specific cases
with clear, distinct error messages.
Updated issueBook
Function
I have added a
check to distinguish between a book that does not exist in the database versus a book that is out of stock.
C
void issueBook() { int id, found = 0; printf("\nEnter Book ID to issue: "); scanf("%d", &id); for (int index = 0; index < totalRecord; index++) { if (book[index].bookID == id) { found = 1; // Condition (i) solved: The book IS in the library // Condition (ii): Check if quantity is 0 if (book[index].noOfCopies > 0) { book[index].noOfCopies--; printf("Success: Book '%s' issued! Remaining: %d\n", book[index].title, book[index].noOfCopies); } else { // Unusual condition: Book exists, but quantity is 0 printf("Error: The book '%s' is currently out of stock (Quantity: 0).\n", book[index].title); } break; } } // Condition (i): If the loop finishes and 'found' is still 0 if (!found) { printf("Error: Book ID %d does not exist in our library records.\n", id); }}🔍 Logic Flow
Comparison
The table below
explains how the code differentiates between your two requested scenarios:
|
Scenario |
found variable |
noOfCopies |
Resulting Message |
|
Book not in Library |
Remains |
N/A |
"Book ID does not
exist" |
|
Book exists but 0 copies |
Becomes |
Is |
"Out of stock" |
|
Normal Issue |
Becomes |
Greater than |
"Book issued!" |
💡
Pro-Tip for "Unusual Conditions"
In a
real-world scenario, you might also want to handle input validation. For example, what happens if the
user types a letter (like 'A') instead of a Book ID number?
To handle that
"unusual condition," you can check the return value of scanf:
C
if (scanf("%d", &id) != 1) { printf("Invalid input! Please enter a numeric ID.\n"); while(getchar() != '\n'); // Clear the input buffer return;}Would you
like me to help you integrate a Search
by Author feature, or perhaps show you how to delete a record entirely if a book is permanently
removed from the library?
Example 9.12
Program in C to demonstrate dynamic memory allocation of structure variables.
(DYNAMIC MEMORY ALLOCATION FOR STRUCTURES)
Answer:
#include<stdio.h>
int main()
{
struct Student_Info
{
int roll;
int age;
};
struct Student_Info *ptr;
int studentCount;
printf( "\n How many students? " );
scanf( "%d", &studentCount );
ptr = (struct Student_Info*) malloc ( studentCount *
sizeof(struct Student_Info) );
for ( int index = 0; index < studentCount; index++ )
{
printf( " \n Enter roll for student %d ", index );
scanf(" %d", &(ptr+index)->roll );
printf( " \n Enter age for student %d ", index );
scanf( "%d", &(ptr+index)->age );
}
printf( "\n Entered details are as follows: \n \n " );
for ( int index = 0; index < studentCount; index++ )
{
printf( " \n Roll for student %d is %d", index,
(ptr+index)->roll );
printf( " \n Age for student %d is %d", index,
(ptr+index)->age );
}
return 0;
}
Meaning
& Output by AI:
This C program
dynamically allocates memory for an array of struct Student_Info structures based on user input for studentCount, then populates and prints the roll and age for each student using pointer arithmetic ((ptr+index)->member). It demonstrates proper use of malloc, structures, and pointers, though it lacks
a free(ptr) call.
C
#include<stdio.h>
#include<stdlib.h> // Included for malloc
int
main()
{
struct Student_Info
{
int
roll;
int
age;
};
struct Student_Info *ptr;
int studentCount;
printf( "\n How many students? "
);
scanf( "%d",
&studentCount );
// Dynamically allocate memory based on
studentCount
ptr = (struct Student_Info*) malloc (
studentCount * sizeof(struct Student_Info) );
// Check if memory allocation was
successful
if (ptr == NULL) {
printf("Memory
allocation failed!");
return
1;
}
// Input data for each student
for ( int
index = 0; index < studentCount; index++
)
{
printf( "
\n Enter roll for student %d ", index );
scanf("
%d", &(ptr+index)->roll );
printf( "
\n Enter age for student %d ", index );
scanf( "%d",
&(ptr+index)->age );
}
// Output data for each student
printf( "\n Entered details are as
follows: \n \n " );
for ( int
index = 0; index < studentCount; index++
)
{
printf( " \n
Roll for student %d is %d", index, (ptr+index)->roll );
printf( "
\n Age for student %d is %d", index, (ptr+index)->age );
}
// Free allocated memory
free(ptr);
return 0;
}
Key Improvements and
Considerations:
·
#include<stdlib.h>: Added to
use malloc and free.
·
free(ptr): Added at the end to prevent memory
leaks.
·
Memory
Check: Added if (ptr == NULL) to ensure malloc succeeded.
Output:
The provided C program
dynamically allocates memory for a user-defined number of students, prompts for
their roll numbers and ages, and then prints the entered details. Assuming the
user inputs 2 students with details (Roll 101, Age 20) and (Roll 102, Age 21),
the output will be:
text
How many students? 2
Enter roll for student 0 101
Enter age for student 0 20
Enter roll for student 1 102
Enter age for student 1 21
Entered details are as follows:
Roll for student 0 is 101
Age for student 0 is 20
Roll for student 1 is 102
Age for student 1 is 21
Note: The program is
missing #include <stdlib.h> for malloc and does not call free(ptr) to release the allocated memory.
Quick activities
1. Extend the above program to include student names in the structure.
Answer:
To include student names, you need
to add a character array (string) to your structure. I have also cleaned
up the syntax slightly to make it more readable by using array notation (ptr[index]) instead of pointer arithmetic.
Here is the extended version of your
program:
C
#include
<stdio.h>
#include
<stdlib.h> // Included for malloc and free
int
main() {
struct Student_Info {
char name[50]; // Added field for
student name
int roll;
int age;
};
struct Student_Info *ptr;
int studentCount;
printf("\n How many students? ");
scanf("%d", &studentCount);
// Dynamically allocate memory
ptr = (struct Student_Info*) malloc(studentCount
* sizeof(struct Student_Info));
// Check if memory allocation was
successful
if (ptr == NULL) {
printf("Memory
allocation failed!\n");
return 1;
}
// Input data for each student
for (int index = 0; index <
studentCount; index++) {
printf("\n--- Data for Student %d
---", index + 1);
printf("\n Enter Name: ");
scanf("%s", ptr[index].name);
// Read string (no & needed for arrays)
printf(" Enter roll: ");
scanf("%d",
&ptr[index].roll);
printf(" Enter age: ");
scanf("%d",
&ptr[index].age);
}
// Output data for each student
printf("\n\n Entered details are as
follows: \n");
printf("-----------------------------------------");
for (int index = 0; index <
studentCount; index++) {
printf("\n Student %d: Name: %s,
Roll: %d, Age: %d",
index + 1, ptr[index].name,
ptr[index].roll, ptr[index].age);
}
printf("\n-----------------------------------------\n");
// Free allocated memory
free(ptr);
ptr = NULL;
return 0;
}
Key
Changes Made:
- char name[50];:
Added a character array to the structure. This allocates 50 bytes per
student for their name.
- ptr[index].name:
In the scanf for the name, we don't use the &
symbol because the name of a character array already acts as a pointer to
its first element.
- Array Notation:
I swapped (ptr+index)->roll for ptr[index].roll. Both work exactly the same way in C, but the latter
is generally easier to read and less prone to typing errors.
- Memory Layout:
By adding the name array, each "block" in memory for a student
is now larger. The sizeof(struct Student_Info) automatically calculates this new size for malloc.
Would you like to see how to handle
names with spaces (like "John Doe") using fgets instead of scanf?
OUTPUT:
Based on the code
provided, here is an example of what the execution would look like assuming you
enter data for 2 students.
Sample Input/Output
text
How many students? 2
--- Data for Student 1 ---
Enter Name: Alice
Enter roll: 101
Enter age: 20
--- Data for Student 2 ---
Enter Name: Bob
Enter roll: 102
Enter age: 21
Entered details are as follows:
-----------------------------------------
Student 1: Name: Alice, Roll: 101, Age: 20
Student 2: Name: Bob, Roll: 102, Age: 21
-----------------------------------------
Explanation of Execution
1.
"How
many students?": You enter 2.
2.
Memory
Allocation: malloc allocates space for 2 struct Student_Info structures on the heap.
3.
Loop
1 (Input):
1.
Iterates 2 times.
2.
scanf("%s", ...) reads the name (Note: %s only reads until the first space).
3.
scanf("%d", ...) reads the integer roll and age.
4.
Loop
2 (Output):
0.
Iterates 2 times to
print the data stored in ptr[0] and ptr[1].
5.
free(ptr):
The dynamically allocated memory is released.
Answer:
Example 9.7: A C program demonstrating an array of structure variables.
#include<stdio.h>
int main()
{
struct Student_Info
{
char name[30];
int roll;
int age;
};
struct Student_Info
student[10];
int studentCount;
printf( "\n
How many students? " );
scanf(
"%d", &studentCount );
for ( int index =
0; index < studentCount; index++ )
{
printf( " \n Enter name for student %d ", index );
getchar();
gets( student[index].name );
printf( " \n Enter roll for student %d ", index );
scanf(" %d", &student[index].roll );
printf( " \n Enter age for student %d ", index );
scanf( "%d", &student[index].age );
}
printf( "\n
Entered details are as follows: \n \n " );
for ( int index =
0; index < studentCount; index++ )
{
printf("\n\n Student %d: ", index+1 );
printf("\n Name: ");
puts( student[index].name );
printf( "
Roll: %d", student[index].roll );
printf( " \n Age: %d", student[index].age );
}
return 0;
}
Note: This is a solid foundational C program for managing student records using an array of structures. It covers the basics: defining a template, capturing user input, and iterating through the data to display it.
Rewrite the program Example 9.7 using dynamic memory allocation
strategy. :
Switching to dynamic memory
allocation is a great move. It makes your program much more flexible because
you no longer have to hard-code a limit like student[10]. Instead, you can request exactly the amount of memory you
need from the Heap at runtime.
Key
Changes
- Pointers:
We replace the array student[10] with a pointer struct
Student_Info *student.
- malloc(): We use this function to allocate memory based on the
user's input.
- free(): Crucial step! Since the system doesn't automatically
reclaim heap memory, we must manually release it before the program ends.
- Arrow Operator (->):
While you can still use student[index].name, if you were passing a single pointer to a function,
you'd use the -> operator to access members.
The
Optimized Code
C
#include
<stdio.h>
#include
<stdlib.h> // Required for malloc and free
struct
Student_Info {
char name[30];
int roll;
int age;
};
int
main() {
struct Student_Info *student; // Pointer to
hold the base address
int studentCount;
printf("\nHow many students would you
like to enter? ");
if (scanf("%d",
&studentCount) != 1 || studentCount <= 0) {
printf("Please enter a valid
number of students.\n");
return 1;
}
// Allocate memory dynamically
student = (struct Student_Info
*)malloc(studentCount * sizeof(struct Student_Info));
// Check if memory allocation was
successful
if (student == NULL) {
printf("Memory allocation failed!
Exiting...\n");
return 1;
}
for (int index = 0; index <
studentCount; index++) {
printf("\n--- Entering Data for Student
%d ---", index + 1);
printf("\nName: ");
getchar(); // Clear newline buffer
fgets(student[index].name,
sizeof(student[index].name), stdin);
printf("Roll: ");
scanf("%d",
&student[index].roll);
printf("Age: ");
scanf("%d",
&student[index].age);
}
printf("\n\n--- Displaying Records
---");
for (int index = 0; index <
studentCount; index++) {
printf("\nStudent %d: %sRoll: %d |
Age: %d\n",
index + 1,
student[index].name,
student[index].roll,
student[index].age);
}
// VERY IMPORTANT: Free the allocated
memory
free(student);
printf("\nMemory successfully
released.\n");
return 0;
}
Why
use sizeof(struct Student_Info)?
When using malloc, you have to tell the computer exactly how many bytes
you want. Since the size of a structure can vary depending on the operating
system or compiler padding, we use sizeof to calculate it accurately.
Quick
Tips for Dynamic Memory:
- Always check for NULL: If
you try to allocate more memory than the RAM has available, malloc
returns NULL. Trying to write to a NULL pointer will cause a segmentation fault (crash).
- The "Leak" Rule: For every malloc(), there should be a free(). If you forget to free(), your program will consume more and more memory over
time—this is a "memory leak."
Would you like to see how to use realloc() to add more students to this list without restarting the
program?
Output of the program:
Since I can't run the code on your
physical machine, I’ve simulated a typical terminal session based on the
dynamic memory version of the program.
In this scenario, we are entering 2
students. Notice how the program handles the name with spaces and the
memory cleanup at the end.
Terminal
Execution Simulation
Plaintext
How
many students would you like to enter? 2
---
Entering Data for Student 1 ---
Name:
Alan Turing
Roll:
101
Age:
41
---
Entering Data for Student 2 ---
Name:
Ada Lovelace
Roll:
102
Age:
36
---
Displaying Records ---
Student
1: Alan Turing
Roll:
101 | Age: 41
Student
2: Ada Lovelace
Roll:
102 | Age: 36
Memory
successfully released.
What's
happening behind the scenes?
When you run this, the computer
follows a specific workflow in its memory. Here is a breakdown of the
"lifecycle" of your data during that output:
- Allocation:
When you type 2,
the program calculates $2 \times \text{sizeof(Student\_Info)}$ and carves
out that exact block in the heap.
- Buffer Clearing:
The getchar() absorbs the "Enter" key you pressed after
typing 2,
ensuring fgets doesn't immediately grab an empty newline.
- Data Storage:
The strings and integers are packed into the allocated heap block.
- Cleanup:
Once the for loop prints the data, free(student) tells the OS, "I'm done with this memory; others
can use it now."
Common
Output Issues to Watch For:
- Trailing Newlines:
You might notice a small gap after the names in the output. This is
because fgets() captures the \n character when you hit Enter.
- Garbage Values:
If you entered "3" but only filled out "2" records,
the third record would display "Garbage Values" (random symbols
or numbers) because that memory wasn't initialized.
Would
you like me to show you a quick trick to remove that annoying newline character
from the names using strtok or a simple if
statement?
Example 9.8 A
C program for handling information about books.
#include<stdio.h>
int main()
{
struct Book_Info
{
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct Book_Info
book[10];
int totalBookCount;
printf( "\n
How many books in total? " );
scanf(
"%d", &totalBookCount );
for ( int index =
0; index < totalBookCount; index++ )
{
printf( " \n Enter title of the book %d ", index );
getchar();
gets(book[index].title);
printf( " \n Enter Book ID %d ", index );
scanf(" %d", &book[index].bookID );
printf( " \n Enter total no of copies %d ", index );
scanf(" %d", &book[index].noOfCopies );
printf( " \n Enter author of the book %d ", index );
getchar();
gets(book[index].author);
}
printf( "\n
Entered details are as follows: \n \n " );
for ( int index =
0; index < totalBookCount; index++ )
{
printf("\n
Book %d \n", index);
printf(" Title: ");
puts( book[index].title );
printf( " ID: %d \n Available copies: %d ",
book[index].bookID,
book[index].noOfCopies );
printf(" \n Author: ");
puts( book[index].author );
}
return 0;
}
Note by AI: It looks like you've put together a solid foundation
for a book management system! You are using an array of structures,
which is exactly the right approach for handling a collection of related data
in C.
Rewrite the program of Example 9.8 using dynamic memory allocation strategy.
Switching to dynamic memory
allocation is a smart move. In your previous code, you were limited to
exactly 10 books. By using malloc(), you can allocate exactly the amount of memory you need at
runtime based on the user's input.
In C, we use a pointer to our
structure and then allocate a block of memory equivalent to $n \times
\text{sizeof(struct)}$.
Memory
Management Strategy
When you use malloc, you are grabbing space from the Heap rather than
the Stack. This allows your program to handle much larger datasets than
a standard local array might allow.
Dynamic
Memory Implementation
C
#include
<stdio.h>
#include
<stdlib.h> // Required for malloc and free
#include
<string.h>
struct
Book_Info {
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
int
main() {
struct Book_Info *books; // Pointer to
structure
int totalBookCount;
printf("\nHow many books would you like
to enter? ");
if (scanf("%d",
&totalBookCount) != 1 || totalBookCount <= 0) {
printf("Invalid input.\n");
return 1;
}
// 1. Allocate memory dynamically
books = (struct Book_Info
*)malloc(totalBookCount * sizeof(struct Book_Info));
// Check if memory allocation was
successful
if (books == NULL) {
printf("Memory allocation failed!
Exiting...\n");
return 1;
}
while (getchar() != '\n'); // Clear buffer
// 2. Input data using pointer arithmetic
or array notation
for (int i = 0; i < totalBookCount; i++)
{
printf("\n--- Book %d ---", i
+ 1);
printf("\nEnter title: ");
fgets(books[i].title, 30, stdin);
books[i].title[strcspn(books[i].title,
"\n")] = 0;
printf("Enter Book ID: ");
scanf("%d",
&books[i].bookID);
printf("Enter total no of copies:
");
scanf("%d",
&books[i].noOfCopies);
while (getchar() != '\n');
printf("Enter author: ");
fgets(books[i].author, 30, stdin);
books[i].author[strcspn(books[i].author, "\n")] = 0;
}
// 3. Display the details
printf("\n--- Library Records ---
\n");
for (int i = 0; i < totalBookCount; i++)
{
printf("\n[%d] %s by %s (ID: %d |
Copies: %d)",
i + 1, books[i].title,
books[i].author,
books[i].bookID,
books[i].noOfCopies);
}
// 4. Crucial step: Free the allocated
memory
free(books);
printf("\n\nMemory successfully
released.\n");
return 0;
}
What
changed?
- The Pointer:
Instead of book[10], we use struct
Book_Info *books. This pointer stores the
starting address of our allocated memory block.
- The Allocation:
malloc(totalBookCount * sizeof(struct Book_Info)) calculates the exact bytes needed.
- The Null Check:
It's a best practice to check if malloc returned NULL. If the system is out of memory, your program should
fail gracefully rather than crashing.
- The Cleanup:
Unlike an array, heap memory isn't automatically cleaned up. We use free(books)
to return the memory to the system once the program is done with it.
Would you like to see how to use realloc to add more books to this list later without restarting the
program?
OUTPUT by AI:
This output assumes that chose to
enter 2 books. Notice how the program handles the strings and integers
smoothly without skipping any inputs.
Simulated
Console Output
Plaintext
How
many books would you like to enter? 2
---
Book 1 ---
Enter
title: The Great Gatsby
Enter
Book ID: 101
Enter
total no of copies: 5
Enter
author: F. Scott Fitzgerald
---
Book 2 ---
Enter
title: 1984
Enter
Book ID: 102
Enter
total no of copies: 3
Enter
author: George Orwell
---
Library Records ---
[1]
The Great Gatsby by F. Scott Fitzgerald (ID: 101 | Copies: 5)
[2]
1984 by George Orwell (ID: 102 | Copies: 3)
Memory
successfully released.
How
the Memory Looks Under the Hood
When you run this dynamic version,
the computer sets up the memory differently than a standard array. Instead of a
fixed "box" of 10 slots, it creates exactly what you asked for on the
Heap.
Why
this output is "cleaner"
- No Buffer Slop:
By using while (getchar() != '\n');, we ensure that the "Enter" key pressed
after typing 101 doesn't accidentally get swallowed by the
"Author" field.
- Formatted Strings:
The fgets and strcspn combo ensures that the author's name doesn't have a
random line break in the middle of your sentence.
Would
you like me to add a feature to save this output to a .txt file so the data persists after you close the program?
Example 9.9 A
C program for handling information about books with an issue operation.
#include<stdio.h>
struct Book_Info
{
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct Book_Info book[10];
int totalRecord;
void input()
{
for ( int index =
0; index < totalRecord; index++ )
{
printf( " \n Enter title of the book %d ", index );
gets(book[index].title);
printf( " \n Enter Book ID %d ", index );
scanf(" %d", &book[index].bookID );
printf( " \n Enter total no of copies %d ", index );
scanf(" %d", &book[index].noOfCopies );
printf( " \n Enter author of the book %d ", index );
getchar();
gets(book[index].author);
}
}
void output()
{
printf( "\n
Entered details are as follows: \n \n " );
for ( int index =
0; index < totalRecord; index++ )
{
printf("\n Book %d \n\n", index);
printf(" Title: ");
puts(book[index].title);
printf( " ID: %d \n Remaining copies: %d \n",
book[index].bookID,
book[index].noOfCopies );
printf(" Authors: ");
puts(book[index].author);
}
}
void issue()
{
int id;
printf("\n
Enter the ID of the book to issue: ");
scanf("%d",
&id);
for ( int index =
0; index < totalRecord; index++ )
{
if( book[index].bookID == id )
{
book[index].noOfCopies = book[index].noOfCopies - 1;
printf("\n One book issued with ID: %d", id);
output();
return;
}
}
}
int main()
{
printf( "\n
How many books in total? " );
scanf(
"%d", &totalRecord );
getchar();
input();
output();
issue();
return 0;
}
Note by AI on the above program:
This is a solid start for a library management program! You’ve correctly used an array of structures to store your data and implemented basic CRUD (Create, Read, Update) logic.
Rewrite the programs of 9.9 using dynamic memory
allocation strategy.:
Switching to Dynamic Memory
Allocation (DMA) is a smart move. It allows your program to be flexible:
instead of being locked into a maximum of 10 books, you can request exactly the
amount of memory you need at runtime.
🧠
The Logic Change
Instead of struct
Book_Info book[10], we will use a pointer (struct
Book_Info *book). We'll then use malloc() to reserve a block of memory on the Heap based on
the user's input.
💻
Updated Code with DMA
C
#include
<stdio.h>
#include
<stdlib.h> // Required for malloc and free
#include
<string.h>
struct
Book_Info {
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
//
Function prototypes
void
clearBuffer();
void
input(struct Book_Info *book, int totalRecord);
void
output(struct Book_Info *book, int totalRecord);
void
issue(struct Book_Info *book, int totalRecord);
int
main() {
int totalRecord;
struct Book_Info *book; // Pointer to hold
the base address
printf("How many books would you like
to register? ");
if (scanf("%d", &totalRecord)
!= 1) return 1;
clearBuffer();
// Allocate memory dynamically
book = (struct Book_Info
*)malloc(totalRecord * sizeof(struct Book_Info));
// Check if memory allocation was
successful
if (book == NULL) {
printf("Memory allocation failed!
Exiting...\n");
return 1;
}
input(book, totalRecord);
output(book, totalRecord);
issue(book, totalRecord);
// CRITICAL: Always free dynamically
allocated memory
free(book);
printf("\nMemory freed.
Goodbye!\n");
return 0;
}
void
clearBuffer() {
int c;
while ((c = getchar()) != '\n' && c
!= EOF);
}
void
input(struct Book_Info *book, int totalRecord) {
for (int i = 0; i < totalRecord; i++) {
printf("\n--- Book %d ---\n",
i + 1);
printf("Title: ");
fgets(book[i].title, 30, stdin);
book[i].title[strcspn(book[i].title,
"\n")] = 0;
printf("ID: ");
scanf("%d",
&book[i].bookID);
printf("Copies: ");
scanf("%d", &book[i].noOfCopies);
clearBuffer();
printf("Author: ");
fgets(book[i].author, 30, stdin);
book[i].author[strcspn(book[i].author,
"\n")] = 0;
}
}
void
output(struct Book_Info *book, int totalRecord) {
printf("\n%-10s %-20s %-20s
%-10s\n", "ID", "Title", "Author",
"Copies");
printf("------------------------------------------------------------\n");
for (int i = 0; i < totalRecord; i++) {
printf("%-10d %-20s %-20s
%-10d\n",
book[i].bookID, book[i].title,
book[i].author, book[i].noOfCopies);
}
}
void
issue(struct Book_Info *book, int totalRecord) {
int id, found = 0;
printf("\nEnter ID to issue: ");
scanf("%d", &id);
for (int i = 0; i < totalRecord; i++) {
if (book[i].bookID == id) {
found = 1;
if (book[i].noOfCopies > 0) {
book[i].noOfCopies--;
printf("Success! Issued:
%s\n", book[i].title);
} else {
printf("Error: No copies
available.\n");
}
break;
}
}
if (!found) printf("ID not
found.\n");
}
🔑
Key Concepts Used
- malloc():
This allocates a block of memory of a specific size. We calculate the size
using $totalRecord \times \text{sizeof(struct Book\_Info)}$.
- The Null Check:
Always check if (book == NULL). If the computer is out of RAM, malloc
returns NULL, and trying to write to it will crash your system.
- free():
This is the most important part of DMA. Unlike local variables that
disappear when a function ends, heap memory stays until you manually
release it. Forgetting this leads to memory leaks.
Would you like to see how to use realloc() so you can add more books later without restarting the
program?
OUTPUT by AI:
Since I can't "run" the
code in a live terminal for you, I’ve simulated exactly how the interaction
would look on your screen based on the Dynamic Memory Allocation version
of the program.
🖥️
Simulated Terminal Output
Plaintext
How
many books would you like to register? 2
---
Book 1 ---
Title:
The Great C Basics
ID:
101
Copies:
5
Author:
Ritchie Dennis
---
Book 2 ---
Title:
Data Structures 101
ID:
102
Copies:
2
Author:
Alan Turing
ID Title Author Copies
------------------------------------------------------------
101 The Great C Basics Ritchie Dennis 5
102 Data Structures 101 Alan Turing 2
Enter
ID to issue: 102
Success!
Issued: Data Structures 101
Memory
freed. Goodbye!
🔍
What happened behind the scenes?
- Memory Allocation:
The moment you typed 2, the program calculated that it needed space for two Book_Info
structures and reserved that exact spot on the Heap.
- Input Phase:
It gathered your strings and integers. Notice that the titles and authors
can have spaces (like "The Great C Basics") because we used fgets
instead of scanf.
- The "Issue" Update: When you issued ID 102, the program looked through the allocated memory,
found the match, and decremented the noOfCopies for that specific memory address from 2 to 1.
- Cleanup:
Before the program closed, the free(book) command handed that memory back to your operating
system.
⚠️
Common Errors to Watch For
If you try to run this and it
behaves strangely, check these two things:
- ID Mismatch:
If you enter a character (like 'A') when it asks for an ID (integer), the scanf will
fail and the program might enter an infinite loop.
- Buffer Overflow:
If you enter a title longer than 30 characters, fgets will
safely cut it off, protecting your memory from corruption.
Would you like me to add a "Save
to File" feature so your library data doesn't disappear every time you
close the program?
Example 9.10
A C program for handling information about books with an issue operation.
Corrected Code:
c
#include<stdio.h>
#include<string.h> // Required for strcspn
struct
Book_Info {
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct
Book_Info book[10];
int
totalRecord;
// Function to safely
remove newline from fgets
void
stripNewline(char *str) {
str[strcspn(str, "\r\n")] =
0;
}
void
input() {
for ( int
index = 0; index < totalRecord; index++ )
{
printf( "\n
Enter title of the book %d: ", index );
fgets(book[index].title, sizeof(book[index].title), stdin);
stripNewline(book[index].title);
printf( "
Enter Book ID %d: ", index );
scanf("%d",
&book[index].bookID );
printf( "
Enter total no of copies %d: ", index );
scanf("%d",
&book[index].noOfCopies );
getchar(); //
Clear newline from scanf
printf( "
Enter author of the book %d: ", index );
fgets(book[index].author, sizeof(book[index].author), stdin);
stripNewline(book[index].author);
}
}
void
output() {
printf( "\n --- Entered details ---
\n" );
for ( int
index = 0; index < totalRecord; index++ )
{
printf("\n
Book %d", index);
printf("\n
Title: %s", book[index].title);
printf("\n
ID: %d \n Remaining copies: %d", book[index].bookID,
book[index].noOfCopies );
printf("\n
Authors: %s\n", book[index].author);
}
}
void
returnBook() {
int id;
printf("\n Enter the ID of the book to
return: ");
scanf("%d", &id);
for ( int
index = 0; index < totalRecord; index++ )
{
//
Corrected comparison operator: ==
if( book[index].bookID
== id ) {
book[index].noOfCopies++;
printf("
One book is returned with ID: %d\n", id);
output();
return;
}
}
printf("Book ID not found.\n");
}
int
main() {
printf( "How many books in total?
" );
scanf( "%d",
&totalRecord );
getchar(); // Clear buffer
input();
output();
returnBook();
return 0;
}
Note by AI for the program:
This is a solid C program for a basic Library Management
System! You've handled the classic "newline buffer" issue quite well
by using getchar()
after scanf and
creating a custom stripNewline
function.
Rewrite the program of
9.10 using dynamic memory allocation strategy.
Switching to Dynamic Memory Allocation is a great move. It makes your program much more flexible because you aren't locked into a fixed array size of 10. Instead, you can request exactly the amount of memory you need at runtime.
In C, this is primarily handled using malloc()
(Memory Allocation) and free()
to release that memory back to the system when you're done.
Key Changes
1. Pointer
usage: We change struct Book_Info
book[10] to a pointer: struct
Book_Info *book.
2. Heap
Allocation: We use malloc
to reserve space based on user input.
3. Arrow
Operator: When using a pointer to a struct, we use -> instead of . to access members (though in an
array-indexed pointer like book[index].title,
the dot operator still works).
The Dynamic Program
C
#include <stdio.h>#include <stdlib.h> // Required for malloc and free#include <string.h> struct Book_Info { char title[30]; int bookID; int noOfCopies; char author[30];}; // Global pointer to hold our dynamic arraystruct Book_Info *book = NULL;int totalRecord; void stripNewline(char *str) { str[strcspn(str, "\r\n")] = 0;} void input() { for (int index = 0; index < totalRecord; index++) { printf("\n--- Book %d ---\n", index + 1); printf("Enter title: "); fgets(book[index].title, sizeof(book[index].title), stdin); stripNewline(book[index].title); printf("Enter Book ID: "); scanf("%d", &book[index].bookID); printf("Enter total copies: "); scanf("%d", &book[index].noOfCopies); getchar(); // Clear newline printf("Enter author: "); fgets(book[index].author, sizeof(book[index].author), stdin); stripNewline(book[index].author); }} void output() { printf("\n--- Library Records ---\n"); for (int index = 0; index < totalRecord; index++) { printf("\n[%d] Title: %s | ID: %d | Copies: %d | Author: %s", index + 1, book[index].title, book[index].bookID, book[index].noOfCopies, book[index].author); } printf("\n");} int main() { printf("How many books would you like to register? "); if (scanf("%d", &totalRecord) != 1 || totalRecord <= 0) { printf("Invalid input.\n"); return 1; } getchar(); // Dynamic Allocation // We calculate size: (number of books) * (size of one struct) book = (struct Book_Info *)malloc(totalRecord * sizeof(struct Book_Info)); // Check if memory was successfully allocated if (book == NULL) { printf("Memory allocation failed! Exiting...\n"); return 1; } input(); output(); // Clean up: Always free dynamic memory! free(book); book = NULL; printf("\nMemory freed. System closed.\n"); return 0;}
How Memory Changes
In your first version, the memory was allocated on the Stack (fixed and automatic). In this version, it's allocated on the Heap.
Why malloc is powerful
here:
· Efficiency: If you only have 2 books, you only use 2 slots of memory.
· Scalability: If you have 10,000 books, you won't get a "Stack Overflow" error; you just use the available RAM.
· Calculated Size: The formula used is:
$$\text{Total Memory} = \text{totalRecord} \times \text{sizeof(struct Book\_Info)}$$
Important Note: Always remember to call free(book). If you don't, your program will have a memory
leak, which means it occupies RAM even after it's finished running.
Would you like to see how to use realloc so you can add more books to the list
later without restarting the program?
OUTPUT by AI:
Since I can't "run" the
code in a live terminal for you, I have simulated the exact interaction based
on the logic of the dynamic memory version.
If you were to input 2 books,
here is exactly how your terminal screen would look:
Simulated
Terminal Output
Plaintext
How
many books would you like to register? 2
---
Book 1 ---
Enter
title: The Great Gatsby
Enter
Book ID: 101
Enter
total copies: 5
Enter
author: F. Scott Fitzgerald
---
Book 2 ---
Enter
title: 1984
Enter
Book ID: 102
Enter
total copies: 3
Enter
author: George Orwell
---
Library Records ---
[1]
Title: The Great Gatsby | ID: 101 | Copies: 5 | Author: F. Scott Fitzgerald
[2]
Title: 1984 | ID: 102 | Copies: 3 | Author: George Orwell
Memory
freed. System closed.
What’s
happening "Under the Hood"?
When you run this, the program
performs specific operations on the system's memory. Because we used malloc, the operating system looks for a contiguous block of empty
space in the Heap large enough to fit your structs.
Why
this output is "clean":
- Buffer Clearing:
Notice how "Enter title" for Book 2 didn't get skipped? That's
because the getchar() in your code successfully swallowed the \n
(Enter key) hit after typing the number of copies for Book 1.
- String Formatting:
The stripNewline function ensures that when we print the records, the
Author and Title stay on the same line rather than creating accidental
line breaks.
Would you like to try adding a search
function next so you can find a book's details by entering its ID?
Example 9.11
A C program for a mini library application.
#include<stdio.h>
#include<string.h>
struct
Book_Info {
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
struct
Book_Info book[10];
int
totalRecord;
void
input() {
for (int index = 0; index < totalRecord;
index++) {
printf("\n--- Book %d ---",
index + 1);
printf("\nEnter title: ");
// Using %[^\n] reads the full title
including spaces
scanf(" %29[^\n]",
book[index].title);
printf("Enter Book ID: ");
scanf("%d",
&book[index].bookID);
printf("Enter total copies:
");
scanf("%d",
&book[index].noOfCopies);
printf("Enter author: ");
scanf(" %29[^\n]",
book[index].author);
}
}
void
displayAll() {
printf("\n--- Library Records
---");
for (int index = 0; index < totalRecord;
index++) {
printf("\nID: %d | Title: %s |
Copies: %d | Author: %s",
book[index].bookID,
book[index].title,
book[index].noOfCopies,
book[index].author);
}
printf("\n");
}
void
issueBook() {
int id, found = 0;
printf("\nEnter Book ID to issue:
");
scanf("%d", &id);
for (int index = 0; index < totalRecord;
index++) {
if (book[index].bookID == id) {
found = 1;
if (book[index].noOfCopies > 0)
{
book[index].noOfCopies--;
printf("Book issued!
Remaining: %d\n", book[index].noOfCopies);
} else {
printf("Error: Out of
stock!\n");
}
break;
}
}
if (!found) printf("Error: Book ID not
found.\n");
}
void
returnBook() {
int id, found = 0;
printf("\nEnter Book ID to return:
");
scanf("%d", &id);
for (int index = 0; index < totalRecord;
index++) {
if (book[index].bookID == id) {
book[index].noOfCopies++;
printf("Book returned! Total
copies: %d\n", book[index].noOfCopies);
found = 1;
break;
}
}
if (!found) printf("Error: Book ID not
found.\n");
}
int
main() {
printf("How many books to register
(max 10)? ");
scanf("%d", &totalRecord);
input();
int option;
while (1) {
printf("\n1: All | 2: Issue | 3:
Return | 4: Exit\nChoice: ");
scanf("%d", &option);
if (option == 1) displayAll();
else if (option == 2) issueBook();
else if (option == 3) returnBook();
else if (option == 4) break;
else printf("Invalid
choice!\n");
}
return 0;
}
Note by AI of the program:
This is a solid, functional C program for a basic Library Management System. You've handled the fundamental logic of C structures and basic file-less data management quite well.
Rewrite the program of
Example 9.11 using dynamic memory allocation strategy.:
Switching to Dynamic Memory
Allocation is a smart move. It transforms your program from one with a hard
limit of 10 books to one that can handle as many books as your computer's RAM
allows.
In C, this is primarily achieved
using malloc() (Memory Allocation) and free() to give the memory back to the system when you're done.
🧠
How Dynamic Allocation Works
Instead of reserving a fixed block
of 10 slots at compile time, we ask the computer for a specific block of memory
at runtime based on the user's input.
💻
The Updated Program
C
#include
<stdio.h>
#include
<stdlib.h> // Required for malloc and free
#include
<string.h>
struct
Book_Info {
char title[30];
int bookID;
int noOfCopies;
char author[30];
};
//
Global pointer instead of a fixed array
struct
Book_Info *book = NULL;
int
totalRecord;
void
input() {
for (int index = 0; index < totalRecord;
index++) {
printf("\n--- Book %d ---",
index + 1);
printf("\nEnter title: ");
scanf(" %29[^\n]",
book[index].title);
printf("Enter Book ID: ");
scanf("%d",
&book[index].bookID);
printf("Enter total copies:
");
scanf("%d",
&book[index].noOfCopies);
printf("Enter author: ");
scanf(" %29[^\n]",
book[index].author);
}
}
void
displayAll() {
printf("\n--- Library Records
---");
for (int index = 0; index < totalRecord;
index++) {
printf("\nID: %d | Title: %s |
Copies: %d | Author: %s",
book[index].bookID,
book[index].title,
book[index].noOfCopies,
book[index].author);
}
printf("\n");
}
int
main() {
printf("How many books would you like
to register? ");
if (scanf("%d", &totalRecord)
!= 1 || totalRecord <= 0) {
printf("Invalid number of
books!\n");
return 1;
}
// Allocate memory dynamically
// Formula: (Number of items) * (Size of
one item)
book = (struct Book_Info
*)malloc(totalRecord * sizeof(struct Book_Info));
// Safety check: always ensure malloc
didn't return NULL
if (book == NULL) {
printf("Memory allocation failed!
Exiting...\n");
return 1;
}
input();
int option;
while (1) {
printf("\n1: All | 2:
Exit\nChoice: ");
scanf("%d", &option);
if (option == 1) displayAll();
else if (option == 2) break;
else printf("Invalid
choice!\n");
}
// IMPORTANT: Clean up the memory before
the program ends
free(book);
printf("Memory freed.
Goodbye!\n");
return 0;
}
🔑
Key Changes Explained
- The Pointer:
We changed struct Book_Info book[10] to struct Book_Info
*book. This is now a "remote
control" that can point to any size block of memory.
- malloc: The line book
= (struct Book_Info *)malloc(totalRecord * sizeof(struct Book_Info)); asks the OS for exactly enough space to hold the
number of books the user requested.
- The Safety Check:
We check if (book == NULL). If your computer is out of memory, malloc
fails; without this check, your program would crash.
- free(book): This is the most important part of dynamic allocation.
Unlike local arrays, dynamic memory isn't deleted automatically. If you
don't free it, you get a memory leak.
⚡
Pro Tip: realloc
If you wanted to add more
books later without restarting the program, you could use realloc(). It grows the existing block of memory while keeping your
current data intact.
Would you like me to show you how to
add a "Add New Book" feature that uses realloc to expand the list on the fly?
OUTPUT by AI:
Since I can't "run" the
code in a live terminal for you, I have simulated the exact interaction based
on the logic of the dynamic memory version.
Here is what the terminal would look
like if you registered 2 books and then viewed the records.
🖥️
Simulated Terminal Output
Plaintext
How
many books would you like to register? 2
---
Book 1 ---
Enter
title: The C Programming Language
Enter
Book ID: 101
Enter
total copies: 5
Enter
author: Dennis Ritchie
---
Book 2 ---
Enter
title: Clean Code
Enter
Book ID: 102
Enter
total copies: 3
Enter
author: Robert Martin
1:
All | 2: Exit
Choice:
1
---
Library Records ---
ID:
101 | Title: The C Programming Language | Copies: 5 | Author: Dennis Ritchie
ID:
102 | Title: Clean Code | Copies: 3 | Author: Robert Martin
1:
All | 2: Exit
Choice:
2
Memory
freed. Goodbye!
🔍
What’s Happening Behind the Scenes?
When you run this, the program
follows a specific lifecycle in your computer's RAM. Understanding the
"Heap" versus the "Stack" is key to mastering dynamic
allocation.
- Allocation Phase:
When you enter 2,
the program calculates:
$2 \times
\text{sizeof(Book\_Info)}$ bytes. It finds a gap in the Heap memory
large enough to fit that data.
- Input Phase:
The scanf functions write directly into that Heap space.
- Access Phase:
Even though book is a pointer, you can still use book[index]
syntax. C treats pointers and arrays almost identically when accessing
offsets.
- Cleanup Phase:
The free(book) command tells the OS, "I'm done with this
specific block," making it available for other programs (like your
browser or music player).
🛠️
Common Output Errors to Watch For
- Buffer Overrun:
If you try to enter a title longer than 29 characters, the %29[^\n]
will stop reading to prevent a crash.
- Input Mismatch:
If you type "Two" instead of 2 for the book count, the program will trigger the
"Invalid number of books!" error we added.
Would you like me to add a sorting
feature so the output displays the books in alphabetical order by title?
Exercise
1. Define structure in the
context of a programming language. How is it different from an array?
Answer:
A structure (often
called a struct) in programming is a composite data type used to group a
collection of related data items of potentially different data
types under a single name [1, 2]. These items, known as members or fields,
allow for the modeling of real-world entities that have multiple, varied
properties.
A structure differs
from an array in the following key ways:
|
Feature |
Structure (Struct) |
Array |
|
Data
Types |
Stores elements of different (heterogeneous) data types (e.g., an integer id,
a string name, a float salary) [1]. |
Stores elements of the same (homogeneous) data type (e.g., an array of only
integers, or only strings) [3]. |
|
Accessing
Elements |
Elements are accessed by their
unique member
names using the dot (.)
or arrow (->) operators [1]. |
Elements are accessed by
their index (position) using square brackets ([])
[3]. |
|
Purpose |
Used to create a complex, single
unit representing an entity (e.g., a Student record)
[1]. |
Used to store a collection of
similar data items in a linear sequence (e.g., a list of all student IDs)
[3]. |
In summary, a
structure is designed for combining different types of data that logically
belong together to describe a single object, while an array is designed for
collecting multiple items of the exact same type in an ordered list [2, 3].
2. Is structure a built-in data
type? Can we apply basic arithmetic operations such as addition, subtraction to
structure variables? Show with a simple C program.
Answer:
Structure is a user-defined data
type in C, not a built-in one. This means the programmer must define its layout
and members [1].
Basic arithmetic
operations such as addition and subtraction cannot be directly applied to
structure variables as a whole. You must perform these operations on the individual
members of the structure. The language does not inherently understand
how to add or subtract two entire composite structures [1, 2].
Example C Program
The following program
demonstrates how to work with structure members and why direct operations fail.
c
#include
<stdio.h>
// Define a structure
to represent a point in 2D space
struct
Point {
int x;
int y;
};
int
main() {
// Declare and initialize structure
variables
struct Point p1 = {10, 20};
struct Point p2 = {5, 8};
struct Point sum;
// --- Applying arithmetic operations to
individual members ---
// We can add the 'x' members and 'y'
members separately
sum.x = p1.x + p2.x;
sum.y = p1.y + p2.y;
printf("Point 1: (%d, %d)\n",
p1.x, p1.y);
printf("Point 2: (%d, %d)\n",
p2.x, p2.y);
printf("Sum of points: (%d, %d)\n",
sum.x, sum.y); // Output: Sum of points: (15,
28)
// --- Attempting direct arithmetic
operation on structure variables (will cause a compilation error) ---
// If you uncomment the line below, the
program will not compile because
// the '+' operator is not defined for
'struct Point' types.
// sum = p1 + p2; // Error: invalid
operands to binary + (have 'struct Point' and 'struct Point')
return 0;
}
Explanation:
·
The line sum.x = p1.x + p2.x; is valid because p1.x and p2.x are integers (built-in types), for which addition is
defined.
·
The commented-out
line sum
= p1 + p2; is invalid and
results in a compilation error because C does not know how to add two struct Point variables automatically [2].
If you need to treat
structures as single units for operations, you would typically write a custom
function to handle the logic, or in C++, you could overload operators.
3. Identify the members of the
structure from the below code segment.
struct Account
{
char acNo[15] ;
char ifsc[15];
char acType[7];
double balance;
double minBalance;
};
struct Account account1,
account2, account3, account[10];
Answer:
The members of
the struct
Account are defined
within the braces and represent the data components of the structure.
Members:
·
char acNo[15] (Account
Number)
·
char ifsc[15] (IFSC
Code)
·
char acType[7] (Account
Type)
·
double balance (Account
Balance)
·
double minBalance (Minimum Balance)
These members
represent the data type's internal components, while account1, account2, account3, and account[10] are instances (variables) of this
structure.
4. Identify the structure
variables from the below code segment.
struct Account
{
char acNo[15] ;
char ifsc[15];
char acType[7];
double balance;
double minBalance;
};
struct Account account1,
account2, account3, account[10];
Answer:
The structure
variables from the provided code segment are the identifiers declared after the
structure definition, which are instances of the struct Account type.
The structure
variables are:
·
account1
·
account2
·
account3
·
account (this is an
array of 10 struct
Account variables)
5. Consider the structure below
and write statements for the following.
a. to declare a variable of the
structure
b. to display the age of the
teacher
struct Teacher
{
char name[30] ;
int age;
};
Answer:
Based on the struct Teacher definition, here are the requested
statements in C/C++:
a. To declare a
variable of the structure:
c
struct
Teacher t1;
(This creates a
variable named t1 of type struct Teacher.)
b. To display the age
of the teacher:
c
printf("Age:
%d", t1.age);
(This uses the dot
operator (.) to access the age member of the t1 variable.)
Question No. 5. Consider the
structure below and write statements for the following.
a. to declare a variable of the
structure
b. to display the age of the
teacher
struct Teacher
{
char name[30] ;
int age;
};
Answer:
Based on the struct Teacher definition, here are the requested
statements in C/C++:
a. To declare a
variable of the structure:
c
struct
Teacher t1;
(This creates a
variable named t1 of type struct Teacher.)
b. To display the age
of the teacher:
c
printf("Age:
%d", t1.age);
(This uses the dot
operator (.) to access the age member of the t1 variable.)
6. Declare a pointer for
structure Teacher (from Question No. 5.) and dynamically allocate memory for 10
records.
Answer:
To declare a pointer for the Teacher structure and dynamically allocate memory for 10 records, you can use the malloc function (in C) or the new operator (in C++).
1.
Using C (with malloc)
In C, you must include <stdlib.h> and use malloc to request memory from the heap.
C
//
Declare the pointer
struct
Teacher *ptr;
//
Allocate memory for 10 Teacher structures
ptr
= (struct Teacher*) malloc(10 * sizeof(struct Teacher));
2.
Using C++ (with new)
In C++, the process is more
streamlined as the new
operator automatically handles the size calculations.
C++
//
Declare and allocate memory for 10 Teacher objects
Teacher
*ptr = new Teacher[10];
Memory
Visualization
When you allocate memory for 10
records, the pointer ptr
points to the start of a contiguous block of memory large enough to hold 10
individual Teacher structures.
Key
Points to Remember:
- Accessing Data:
Since ptr is a pointer, you access members using the arrow
operator (->) for a single record (e.g., ptr->age)
or array indexing (e.g., ptr[i].age).
- Memory Management:
Always free the memory once you are done to avoid memory leaks:
- In C: free(ptr);
- In C++: delete[]
ptr;
Would you like to see a code example
of how to use a loop to input names and ages into these 10 allocated records?
7. Consider the structure below
and write statements for the following.
a. to declare a pointer for the
above structure and display the salary.
b. to declare a single pointer
for two different variables of the higher structure and display the details of
the employee whose salary is more.
struct Employee
{
char name[30];
double salary;
};
Answer:
Here are the requested C
language statements for the struct
Employee.
a. To declare a pointer
for the above structure and display the salary:
c
struct Employee emp1 = {"John Doe", 55000.50};
struct Employee* empPtr = &emp1; // Declare and initialize pointer
printf("Salary: %.2f\n", empPtr->salary); // Display salary using -> operator
b. To declare a single pointer for two different variables of the higher structure and display the details of the employee whose salary is more:
c
struct Employee emp1 = {"Alice", 60000.0};
struct Employee emp2 = {"Bob", 75000.0};
struct Employee* maxPtr;
// Use pointer to compare and point to the higher salary
maxPtr = (emp1.salary > emp2.salary) ? &emp1 : &emp2; // Display details of the higher paid employee
printf("Higher Salary Employee: %s, Salary: %.2f\n", maxPtr->name, maxPtr->salary);
``` [3]
Question
No. 7. Consider the structure below and write statements for the following.
a.
to declare a pointer for the above structure and display the salary.
b.
to declare a single pointer for two different variables of the higher structure
and display the details of the employee whose salary is more.
struct
Employee
{
char
name[30];
double
salary;
};
8.
Rewrite the program of Question No. 7 to facilitate dynamic memory allocation
for N number of record where N in a user input.
Answer:
To handle a dynamic number of
records based on user input, we use functions like malloc() (from stdlib.h). This allows us to allocate a block of memory on the heap
that acts like an array, but with a size determined at runtime.
Dynamic
Memory Allocation for N Records
Here is the complete program to
achieve this:
C
#include
<stdio.h>
#include
<stdlib.h> // Required for malloc and free
struct
Employee {
char name[30];
double
salary;
};
int
main() {
int n, i;
struct Employee *ptr;
printf("Enter the number of records
(N): ");
scanf("%d", &n);
// Dynamic memory allocation for N records
ptr = (struct Employee*) malloc(n * sizeof(struct
Employee));
// Check if memory allocation was
successful
if (ptr == NULL) {
printf("Memory not allocated.
Exiting...");
return 1;
}
// Input details for N employees
for (i = 0; i < n; i++) {
printf("\nEnter details for
Employee %d:\n", i + 1);
printf("Name: ");
scanf("%s", (ptr +
i)->name);
printf("Salary: ");
scanf("%lf", &(ptr +
i)->salary);
}
// Displaying the records using the pointer
printf("\n--- Employee Records
---\n");
for (i = 0; i < n; i++) {
printf("Employee %d: %s, Salary:
%.2f\n", i + 1, (ptr + i)->name, (ptr + i)->salary);
}
// Deallocate the memory
free(ptr);
return 0;
}
Key
Concepts Used
- malloc(n * sizeof(struct Employee)): This calculates the total bytes required for N
structures and reserves that space in the memory.
- Pointer Arithmetic:
We use (ptr + i) to navigate to the ith record.
- The -> Operator:
Used to access members when dealing with a pointer to a structure.
- free(ptr):
This is a crucial step in dynamic memory management to prevent memory
leaks by releasing the memory back to the system once the program is
done with it.
Would you like me to show you how to
rewrite this using calloc() to automatically initialize the salaries to zero?
OUTPUT OF THE PROGRAM:
To show the output, let’s assume the
user inputs 2 for the number of records. Here is how the console
interaction and output would look:
Sample
Program Execution
Plaintext
Enter
the number of records (N): 2
Enter
details for Employee 1:
Name:
Alice
Salary:
65000.50
Enter
details for Employee 2:
Name:
Bob
Salary:
42000.75
---
Employee Records ---
Employee
1: Alice, Salary: 65000.50
Employee
2: Bob, Salary: 42000.75
Understanding
the Memory Layout
When you run this program, the
computer organizes the data in the Heap section of the memory. Unlike a
standard array where the size is fixed at the start, the pointer ptr points to a flexible block of memory that grows based on
your input $N$.
Key
Observations in the Output:
- Precision:
The salary is displayed with two decimal places because of the %.2f
format specifier.
- Indexing:
Even though C uses 0-based indexing (0 and 1), the output uses i + 1 to
make it more readable for the user (Employee 1 and 2).
- Sequential Storage:
The records are stored contiguously in memory. If ptr
points to address 1000, the second record starts exactly sizeof(struct Employee)
bytes later.
Would you like me to show you how to
add a search feature to this program so you can find a specific employee by
name?
9.
Consider the below structure and design a simple banking system that supports
the following operations.
a.
opening of accounts (Hint: input to the variables)
b.
displaying details based on account number
c.
displaying all account details
d.
displaying details of all accounts whose balance is more than 1000
e.
depositing an amount to an account
f.
withdrawing some amount from an account
struct
Account
{
char
acNo[15] ;
char
ifsc[15];
char
acType[7];
double
balance;
double
minBalance;
};
Answer:
Here is a C program
that designs a simple banking system supporting all the requested operations
using an array of the provided struct Account.
c
#include
<stdio.h>
#include
<string.h>
#include
<stdlib.h>
// Define the Account
structure
struct
Account {
char acNo[15];
char ifsc[15];
char acType[7];
double balance;
double minBalance;
};
// Global array to
store accounts and a counter
struct
Account accounts[100]; // Supports up to 100 accounts
int
numAccounts = 0;
// Function prototypes
void
openAccount();
void
displayDetailsByAcNo();
void
displayAllAccountDetails();
void
displayAccountsAbove1000();
void
depositAmount();
void
withdrawAmount();
int
findAccountIndex(char acNo[]);
int
main() {
int choice;
do {
printf("\n***
Simple Banking System Menu ***\n");
printf("1.
Open an account\n");
printf("2.
Display details based on account number\n");
printf("3.
Display all account details\n");
printf("4.
Display accounts with balance more than 1000\n");
printf("5.
Deposit an amount\n");
printf("6. Withdraw some amount\n");
printf("7.
Exit\n");
printf("Enter
your choice: ");
scanf("%d",
&choice);
switch
(choice) {
case
1:
openAccount();
break;
case
2:
displayDetailsByAcNo();
break;
case
3:
displayAllAccountDetails();
break;
case
4:
displayAccountsAbove1000();
break;
case
5:
depositAmount();
break;
case
6:
withdrawAmount();
break;
case
7:
printf("Exiting
program. Goodbye!\n");
break;
default:
printf("Invalid
choice. Please try again.\n");
}
} while (choice != 7);
return 0;
}
// Helper function to
find the index of an account by its account number
int
findAccountIndex(char acNo[]) {
for (int
i = 0; i < numAccounts; i++) {
if
(strcmp(accounts[i].acNo, acNo) == 0) {
return
i; // Account found, return its index
}
}
return -1;
// Account not found
}
// a. Opening of
accounts
void
openAccount() {
if (numAccounts < 100) {
printf("Enter
Account Number: ");
scanf("%s",
accounts[numAccounts].acNo);
printf("Enter
IFSC Code: ");
scanf("%s",
accounts[numAccounts].ifsc);
printf("Enter
Account Type (e.g., Sav, Cur): ");
scanf("%s",
accounts[numAccounts].acType);
printf("Enter
Initial Balance: ");
scanf("%lf",
&accounts[numAccounts].balance);
accounts[numAccounts].minBalance = 1000.0; // Set
minimum balance requirement
printf("Account
opened successfully!\n");
numAccounts++;
} else {
printf("Cannot
open more accounts. System limit reached.\n");
}
}
// Helper function to
print account details
void
printAccountDetails(struct Account acc) {
printf("\nAccount Details:\n");
printf("Account Number: %s\n",
acc.acNo);
printf("IFSC Code: %s\n",
acc.ifsc);
printf("Account Type: %s\n",
acc.acType);
printf("Balance: %.2lf\n",
acc.balance);
printf("Minimum Balance: %.2lf\n",
acc.minBalance);
}
// b. Displaying
details based on account number
void
displayDetailsByAcNo() {
char acNo[15];
printf("Enter account number to display
details: ");
scanf("%s", acNo);
int index = findAccountIndex(acNo);
if (index != -1) {
printAccountDetails(accounts[index]);
} else {
printf("Account
not found.\n");
}
}
// c. Displaying all
account details
void
displayAllAccountDetails() {
if (numAccounts > 0) {
printf("\n***
All Account Details ***\n");
for (int i = 0; i < numAccounts; i++) {
printAccountDetails(accounts[i]);
printf("---------------------------\n");
}
} else {
printf("No
accounts to display.\n");
}
}
// d. Displaying
details of all accounts whose balance is more than 1000
void
displayAccountsAbove1000() {
int count = 0;
printf("\n*** Accounts with Balance
> 1000 ***\n");
for (int
i = 0; i < numAccounts; i++) {
if
(accounts[i].balance > 1000.0) {
printAccountDetails(accounts[i]);
printf("---------------------------\n");
count++;
}
}
if (count == 0)
{
printf("No
accounts found with balance more than 1000.\n");
}
}
// e. Depositing an
amount to an account
void
depositAmount() {
char acNo[15];
double amount;
printf("Enter account number to deposit
into: ");
scanf("%s", acNo);
int index = findAccountIndex(acNo);
if (index != -1) {
printf("Enter
amount to deposit: ");
scanf("%lf",
&amount);
if
(amount > 0) {
accounts[index].balance += amount;
printf("Amount
deposited successfully. New balance is %.2lf.\n",
accounts[index].balance);
} else
{
printf("Invalid amount. Must be positive.\n");
}
} else {
printf("Account
not found.\n");
}
}
// f. Withdrawing some
amount from an account
void
withdrawAmount() {
char acNo[15];
double amount;
printf("Enter account number to
withdraw from: ");
scanf("%s", acNo);
int index = findAccountIndex(acNo);
if (index != -1) {
printf("Enter
amount to withdraw: ");
scanf("%lf",
&amount);
// Basic
validation for positive amount and sufficient balance
if
(amount > 0 &&
accounts[index].balance - amount >= accounts[index].minBalance) {
accounts[index].balance -= amount;
printf("Amount
withdrawn successfully. New balance is %.2lf.\n",
accounts[index].balance);
} else
if (amount <= 0)
{
printf("Invalid
amount. Must be positive.\n");
} else
{
printf("Insufficient
funds or minimum balance requirement not met (min: %.2lf).\n",
accounts[index].minBalance);
}
} else {
printf("Account
not found.\n");
}
}
The code provides a
complete, menu-driven C program to perform all the specified banking
operations, managing the accounts in a global array of the defined struct Account.
This system stores all
data in memory and will reset upon program termination.
Would you like me to
show you how to save account data to a file so it persists
between runs? Shall we implement file handling next?
Meaning & OUTPUT:
This C program is a well-structured implementation of a Simple Banking System. It uses a struct
to represent bank accounts and an array to manage them.
Here is a breakdown of how the code works and a few suggestions to make it even more robust.
1. Code Architecture
The program is organized into three main layers:
1.
Data
Structure: The struct Account stores the core data
(Account Number, IFSC, Type, Balance, and Min Balance).
2.
Helper
Functions: findAccountIndex and printAccountDetails
are used to keep the main logic clean and avoid repeating code.
3. Core Features: Functions for opening, searching, listing, depositing, and withdrawing.
2. Key Components Explained
|
Feature |
Logic Used |
|
Account Storage |
Uses a global array |
|
Searching |
Uses |
|
Withdrawal Rule |
Enforces a minimum balance
check: |
|
Validation |
Checks if the amount is
positive for both deposits and withdrawals. |
3. Potential Improvements
While the code is functionally sound, here are a few
"pro-tips" for a C programmer:
·
Buffer Overflow Protection: Instead of scanf("%s",
...);, use scanf("%14s",
...);. This prevents the user from entering more characters than
the array can hold (which causes crashes).
·
Case Sensitivity: strcmp is
case-sensitive. If a user enters "sav" but the account was opened as
"Sav", it might cause confusion. You could use strcasecmp
(on Linux) or convert input to lowercase.
·
Data Persistence: Currently, all data is lost when the
program closes. To make this a "real" app, you would eventually want
to use File I/O (fopen,
fwrite, fread) to
save the accounts to a .dat or .txt file.
4. Quick Fix for a Small Syntax Error
In the code you provided, there are a couple of spacing
issues likely caused by copy-pasting (e.g., case1:, case2:,
and elseif). Make sure there is a space:
C
// Change this:case1: // To this:case 1: // And change this:elseif (amount <= 0)// To this:else if (amount <= 0)Would you like me to show you how to add File I/O so that your account
data stays saved even after you close the program?
OUTPUT (by AI):
Since I cannot run the code directly
on your machine, I have simulated the program's execution based on the logic
provided. This demonstrates a typical "session" where an account is
opened, a deposit is made, and the details are viewed.
Simulated
Program Output
Plaintext
***
Simple Banking System Menu ***
1.
Open an account
2.
Display details based on account number
3.
Display all account details
4.
Display accounts with balance more than 1000
5.
Deposit an amount
6.
Withdraw some amount
7.
Exit
Enter
your choice: 1
Enter
Account Number: 101ABC
Enter
IFSC Code: BANK0001
Enter
Account Type (e.g., Sav, Cur): Sav
Enter
Initial Balance: 1500
Account
opened successfully!
***
Simple Banking System Menu ***
1.
Open an account
2.
... (other options)
Enter
your choice: 5
Enter
account number to deposit into: 101ABC
Enter
amount to deposit: 500
Amount
deposited successfully. New balance is 2000.00.
***
Simple Banking System Menu ***
1.
Open an account
2.
... (other options)
Enter
your choice: 2
Enter
account number to display details: 101ABC
Account
Details:
Account
Number: 101ABC
IFSC
Code: BANK0001
Account
Type: Sav
Balance:
2000.00
Minimum
Balance: 1000.00
***
Simple Banking System Menu ***
1.
Open an account
...
Enter
your choice: 7
Exiting
program. Goodbye!
How
the Data is Managed (Memory View)
When you run this program, the
computer allocates a block of memory for the accounts array. Here is a conceptual look at how the data is
organized in the system's memory after the session above:
|
Index |
acNo |
ifsc |
balance |
numAccounts (Counter) |
|
0 |
"101ABC" |
"BANK0001" |
2000.00 |
1 |
|
1 |
(Empty) |
(Empty) |
0.00 |
|
|
... |
... |
... |
... |
Logic
Check: The Withdrawal Limit
If you were to try and withdraw 1500
from the account above, the program would stop you:
- Current Balance:
2000.00
- Withdrawal Request:
1500.00
- Calculation:
$2000 - 1500 = 500$
- Result:
Since 500 is less than the Minimum Balance (1000), the withdrawAmount()
function will trigger the else block and deny the transaction.
Would you like me to modify the code
to include a Transfer function so you can move money between two
different accounts?